-1

The problem is I am trying to select image from the gallery. After that if I open the original image from the gallery it's not opening (Black screen coming)! Even if I tried to select that picture again from the application, it's giving an error like can't load image.

private void openGalleryForImageSelection()
{
    Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    try
    {
        startActivityForResult(intent, IMAGE_FROM_GALLERY);
    }
    catch(Throwable e)
    {
        Log.e(LOG_TAG,"openGalleryForImageSelection failed",e);
        Toast.makeText(this,getResources().getString(R.string.image_error),Toast.LENGTH_SHORT).show();
    }
}

Please help me how can I resolve this issue. Thank you.

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
Ravi Teja
  • 1
  • 3
  • are you getting image path properly, inside `onActivityResult()` method? – Kalyani Amrutkar Nov 11 '16 at 10:18
  • Thank you for your response. I am getting the image path correctly. Any image first time it's opening. But if I try to select again the same picture again it's not opening! Even if I tried to open same image from outside it's giving a blank image! – Ravi Teja Nov 11 '16 at 10:35

2 Answers2

0

first add these permission to your menifest.

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

now call showFileChooser() function anywhere.

    private void showFileChooser() {
        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
        intent.setType("image/*");
        intent.addCategory(Intent.CATEGORY_OPENABLE);
        try {
            startActivityForResult(
                    Intent.createChooser(intent, "Select a Image to Upload"),
                    1);
        } catch (android.content.ActivityNotFoundException ex) {
            Toast.makeText(getApplicationContext(), "Please install a Photo Viewer.",
                    Toast.LENGTH_SHORT).show();
        }

    }
@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == 1) {
            if (resultCode == Activity.RESULT_OK) {

            }
        }
}
0

Change your openGalleryForImageSelection() method to:

 public void openGalleryForImageSelection(View view) {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), IMAGE_FROM_GALLERY);
}

Then you will get the intent data inside onActivityResult(), then get the real path in it by calling getPathForImage() like this:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 1) {
        if (resultCode == Activity.RESULT_OK) {
         realPath = getPathForImage(ProfileActivity.this, data.getData());
        }
    }
}


 public static String getPathForImage(Context context, Uri uri)
 {
    String result = null;
    Cursor cursor = null;

    try {
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(uri, proj, null, null, null);
        if (cursor == null) {
            result = uri.getPath();
        } else {
            cursor.moveToFirst();
            int column_index = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
            result = cursor.getString(column_index);
            cursor.close();
        }
    }
    catch (Exception e)
    {
        e.printStackTrace();
    }
    finally {
        if (cursor != null) {
            cursor.close();
        }
    }
    return result;
}

And use realPath you got in onActivityResult for converting into Bitmap and load that Bitmap into your ImageView This is the way to do that. Hope it will help you