1
// 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.

Likith Ts
  • 296
  • 1
  • 7
  • 18
  • If it's displaying the wrong image, what image is it displaying? – iRuth Jan 28 '15 at 16:09
  • If I click on first image in gridview. It display last image in gridview. For example if I have image of car,bike, computer and laptop in gridview. If click on bike instead of opening a fullscreen image of bike. It opens a fullscreen image of laptop. But the path which is returned is path of the bike. Only when it is displaying it is showing a wrong image. – Likith Ts Jan 28 '15 at 16:18

1 Answers1

1

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);
}
iRuth
  • 2,737
  • 3
  • 27
  • 32
  • works perfect. Thank You very much. As I mentioned in my question . I'm using viewPager to swipe images inside the imageview. I have share button . When I click on share button i'm able to share the image which I used to open the FullScreen. If I swipe and goto next image, i'm not able to share that image. Can share be enabled for all the images. – Likith Ts Jan 28 '15 at 17:24
  • Please check out my XML file. – Likith Ts Jan 28 '15 at 17:33
  • If you are not able to share the next image, which image does it share? – iRuth Jan 28 '15 at 18:36
  • image which I click inside gridview to open full screen image view. – Likith Ts Jan 28 '15 at 18:59
  • Do you have the `onOptionsItemSelected` method in your activity? – iRuth Jan 28 '15 at 19:56
  • No. U can check out my question . I have given you the ImageDisplayActivity file. – Likith Ts Jan 28 '15 at 20:02
  • No . As I told u above method works only for one image at a time. Means when I click on one image inside gridview. It will return the path of that image only. So above method can share only that image. When I'm inside the FullScreenActivity I have an option of swiping an image from right to left and left to right implement this using ViewPager. So Whenever I do that, I'm not able to get the path of the image which is currenty full screen. If I click on share button i'm able to share the old image not the current image which is displaying in fullscreen. – Likith Ts Jan 28 '15 at 22:39
  • Did you add the `onOptionsItemSelected` method to your activity and it did not work? Or are you using the `onCreateOptionsMenu` method to get the `ShareActionProvider`? – iRuth Jan 28 '15 at 22:48
  • yeah using onOptionsItemSelected and to make share icon apear . I need to add getMenuInflater().inflate(R.menu.display,menu); MenuItem item = menu.findItem(R.id.menu_item_share); This to lines but still the result is same. If remove those two lines, i'm not getting the share icon displayed – Likith Ts Jan 28 '15 at 23:03
  • You should not remove the `onCreateOptionsMenu` method. Your activity should have both the `onCreateOptionsMenu` method and the `onOptionsItemSelected` method. The `onCreateOptionsMenu` method creates the menu items and the `onOptionsItemSelected` method specifies what happens when a menu item is selected. – iRuth Jan 28 '15 at 23:10
  • I have both onCreateOptionsMenu and onOptionsItemSelected , but the result is same. – Likith Ts Jan 28 '15 at 23:33
  • After modify the onCreateOptionMenu . I can see the share icon, but if I click on share nothing happens. Share problem is not solved . Each time when I swipe image inside imageview. I should be able read path of the currect image which is displaying full screen. So I can pass that path to file inside onCreateOptionMenu and make share work for all the image. – Likith Ts Jan 29 '15 at 02:21
  • Did you modify both the `onCreateOptionsMenu` and `onOptionsItemSelected` methods? – iRuth Jan 29 '15 at 02:29
  • Yes I did both. But my onOptionsItemSelected is never running. – Likith Ts Jan 29 '15 at 02:32
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/69779/discussion-between-iruth-and-likith-ts). – iRuth Jan 29 '15 at 02:36
  • iRuth can you help me in fix this. http://stackoverflow.com/questions/28305367/make-camera-preview-better-for-all-the-phones – Likith Ts Feb 03 '15 at 17:46
  • iRuth my preview is too dark in low light. But google camera make preview more better. Can you help me in solving this.http://stackoverflow.com/questions/28429071/camera-preview-is-too-dark-in-low-light-android/28429186?noredirect=1#comment45191046_28429186. – Likith Ts Feb 10 '15 at 11:49
  • i'm facing the issue of opening a wrong image from grid view to full screen image view. I have posted a new question. Please help me out in solving http://stackoverflow.com/q/28742231/4239092 – Likith Ts Feb 26 '15 at 12:19