Open full Screen Image when clicked on grid view a

2020-06-06 06:54发布

// 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);

        }

This is my ImageDisplayActivity class.

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

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 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 class.

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);
}

}

My Touch Image class is working perfectly fine.

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.

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>

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.

please help me in solving the things.

1条回答
干净又极端
2楼-- · 2020-06-06 07:17

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;
}

Replace your intent in your onClick method with

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

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

That should solve the problem.

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);
}
查看更多
登录 后发表回答