在网格视图机器人点击时打开全屏幕图像网格、视图、机器人、全屏幕

2023-09-05 05:09:32 作者:小姐,你身上带有胸器

// This is my On click function which opens New activity for image view.

  @Override
        public void onClick(View v) {
            Intent intent = new Intent(activity, ImageDisplayActivity.class);

            intent.putExtra("id", position+1);

下面我得到它被点击的文件的路径。

Below i'm getting the path of the file which is clicked.

            intent.putExtra("position", String.valueOf(getItem(position)));

            Log.d("ImageAdapter","Intent.putExtra ");
            activity.startActivity(intent);

        }

这是我ImageDisplayActivity类。

This is my ImageDisplayActivity class.

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_imageview);

获取该被点击GridView中文件的路径。

Getting the path of the file which is clicked in gridview.

        path = getIntent().getExtras().getString("position");

        Intent i1 = getIntent();
        utils = new HelperUtils(this);
        imagePaths = utils.getFilePaths();

imagePaths给我的文件夹内的图片,其中的集合。

imagePaths gives the collection of images which inside my folder.

        position = i1.getExtras().getInt("id");

给出的图像的位置。

Gives the position of the image.

        pagerAdapter = new FullScreenImageAdapter(this, imagePaths);
        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(pagerAdapter);
        pager.setCurrentItem(position);


       }


    public boolean onCreateOptionsMenu(Menu menu){

        getMenuInflater().inflate(R.menu.display,menu);
        MenuItem item = menu.findItem(R.id.menu_item_share);
        shareActionProvider = (ShareActionProvider) item.getActionProvider();

        Intent shareIntent = new Intent();

        if(shareActionProvider != null){

            imageFile = new File(path);
            imageUri = Uri.fromFile(imageFile);
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
            shareIntent.setType("image/jpeg");

            if(shareActionProvider != null){

                shareActionProvider.setShareIntent(shareIntent);
                 }

        }

        return true;

    }

FullScreenImageAdapter类。

FullScreenImageAdapter class.

公共类FullScreenImageAdapter扩展PagerAdapter {

public class FullScreenImageAdapter extends PagerAdapter {

private Activity activity;
private ArrayList<String> imagePaths;
private LayoutInflater inflater;

public FullScreenImageAdapter(Activity activity,ArrayList<String> imagePaths){
    this.activity = activity;
    this.imagePaths = imagePaths;
}

@Override
public int getCount() {
    return this.imagePaths.size();
}

@Override
public boolean isViewFromObject(View view, Object object) {
    return view == ((RelativeLayout) object);
}

public Object instantiateItem(ViewGroup container, int position){
    final TouchImageView imageView;

    inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View view = inflater.inflate(R.layout.layout_fullscreen_image, container,false);

    imageView = (TouchImageView) view.findViewById(R.id.fullscreenimage);

    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inPreferredConfig = Bitmap.Config.ARGB_8888;
    Bitmap bitmap = BitmapFactory.decodeFile(imagePaths.get(position), options);
    imageView.setImageBitmap(bitmap);

    ((ViewPager) container).addView(view);


    return view;

}


public void destroyItem(ViewGroup container, int position, Object object){
    (container).removeView((RelativeLayout) object);
}

}

我的触摸Image类是工作完全正常。

My Touch Image class is working perfectly fine.

当我点击一个项目在图像中的GridView,它会显示在全屏幕错误的图像。截至现在,我使用ViewPager滚动图像。我得到的图像的路径时,在全屏打开。因此,只有这个文件,我发现了路上我能够分享。所以,当我滚动影像,我不能够共享图像。 Bcoz我没有图像的路径。

When I click an item in image in gridview, it is displaying a wrong image in full screen. As of now I'm using ViewPager to scroll image. I'm getting the path of the image when open in fullscreen. Hence only that file i'm getting the path i'm able to share. So when I scroll through the images, i'm not able to share images. Bcoz I don't have the path of the image.

这是使用用于启动ImageView的活动的XML文件

This is the XML File using for starting the imageview activity

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#000000">

       <android.support.v4.view.ViewPager
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/pager"/>

</RelativeLayout>

For Full Screen Class

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <com.android.example.TouchImageView
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:id="@+id/fullscreenimage"
        android:scaleType="fitCenter"
        />

</RelativeLayout>

每当我从刷卡离开了我的下面code能够收到FullScreemImageAdapter类执行到右。图片

Whenever I swipe image from left to right my below code is getting executed from FullScreemImageAdapter class.

 public Object instantiateItem(ViewGroup container, int position){
        final TouchImageView imageView;
        inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.layout_fullscreen_image, container,false);
        imageView = (TouchImageView) view.findViewById(R.id.fullscreenimage);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = BitmapFactory.decodeFile(imagePaths.get(position), options);

在这里,我试图让图片的路径。但它的返回图像的错误路径

Here I'm trying to get the path of the image. But its returning a wrong path of the image.

 path = String.valueOf(imagePaths.get(position));
    Log.d("FullScreenImageAdapter", " path " + path);
    imageView.setImageBitmap(bitmap);
    ((ViewPager) container).addView(view);
    return view;
}

所以,如果我能够读取图像的确切路径。我想我会为这个问题的解决方案。

So if i'm able to read the exact path of the image. I think i'll get the solution for the problem.

请帮我解决的事情。

推荐答案

由于您的的getItem 方法返回列表中的其他位置,就应建立另一个方法返回相应的位置上,如下所示。

Since your getItem method returns a different position on the list, you should create another method to return the corresponding position, as shown below.

@Override
public Object getItem(int position) {
    //return this.itemList.get(itemList.size() - 1 - position);
    return this.itemList.get(getPosition(position));
}

public int getPosition(int position) {
    return itemList.size() - 1 - position;
}

@Override
 public void onClick(View v) {
     Intent intent = new Intent(activity, ImageDisplayActivity.class);

     intent.putExtra("id", getPosition(position));
 ...

这应该解决的问题。

要解决的图像共享的问题,您需要修改 onCreateOptionsMenu 方法并添加 onOptionsItemSelected 方法,我已经如下。您应该直接从 imagePaths 列表引用图片的路径。你不需要经由意图从第一活动发送到下一个活动的路径,因为你已经经由意图发送的位置

To solve the image sharing problem, you need to modify the onCreateOptionsMenu method and add the onOptionsItemSelected method as I have shown below. You should reference the image path directly from the imagePaths list. You do not need to send the path from the first activity to the next activity via an intent because you already sent the position via the intent.

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.display,menu);

    // Locate MenuItem with ShareActionProvider
    MenuItem item = menu.findItem(R.id.menu_item_share);

    // Fetch and store ShareActionProvider
    shareActionProvider = (ShareActionProvider) item.getActionProvider();

    //   return true;
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    switch (item.getItemId()) {
    case R.id.menu_item_share:

        Intent shareIntent = new Intent();

        if(shareActionProvider != null){            
            path = imagePaths.get(pager.getCurrentItem());

            imageFile = new File(path);
            imageUri = Uri.fromFile(imageFile);
            shareIntent.setAction(Intent.ACTION_SEND);
            shareIntent.putExtra(Intent.EXTRA_STREAM, imageUri);
            shareIntent.setType("image/jpeg");

            shareActionProvider.setShareIntent(shareIntent);
        }

        return true;
    }
    return super.onOptionsItemSelected(item);
}