0

I want to get a image from Gallery with the share command.

My current code is:

Intent intent = getIntent();
    String action = intent.getAction();
    String type = intent.getType();

    if (Intent.ACTION_SEND.equals(action) && type != null) {
        Log.d("Test","Simple SEND");
        Uri imageUri = (Uri)intent.getParcelableExtra(Intent.EXTRA_STREAM);
        if (imageUri != null) {
            InputStream iStream = getContentResolver().openInputStream(imageUri);
        }
    } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
        Log.d("Test", "Multiple SEND");
    }

The value of the imageUri is: content://media/external/images/media/37

But the function "openInputStream" throws the error "java.io.FileNotFoundException".

With the following function i get the real path of the image.

public static String getRealPathFromUri(Context context, Uri contentUri) {
    Cursor cursor = null;
    try {
        String[] proj = { MediaStore.Images.Media.DATA };
        cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

But i don't know how to convert it to a bitmap.

1 Answers1

0

A Uri is not a File. new File(imageUri.toString()) is always wrong. imageUri.getPath() only works if the scheme of the Uri is file, and in your case, the scheme is probably content.

Use ContentResolver and openInputStream() to get an InputStream on the file or content identified by the Uri. Then, pass that stream to BitmapFactory.decodeStream().

Also, please decode bitmaps on a background thread, so that you do not freeze your UI while this work is going on.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • Thanks for your answer. I added this line: "InputStream iStream = getContentResolver().openInputStream(imageUri);" But that line always throws the error "java.io.FileNotFoundException". – WhatEver2337 May 11 '17 at 13:39
  • @WhatEver2337: What is the value of `imageUri`? – CommonsWare May 11 '17 at 13:41
  • The value is: content://media/external/images/media/37 – WhatEver2337 May 11 '17 at 13:58
  • @WhatEver2337: That should be fine, though it may depend a bit on who is sending you that `Uri`. Consider asking a fresh Stack Overflow question -- or editing your existing one -- with your current code and the complete stack trace from the crash. – CommonsWare May 11 '17 at 14:02
  • 1
    @WhatEver2337: I asked you to post your current code, the one that was crashing. You did not do this. I asked you to post the Java stack trace associated with the crash. You did not do this. And your `getRealPathFromUri()` will not work with many images from the `MediaStore`, let alone for any `Uri` that is *not* from the `MediaStore`. – CommonsWare May 11 '17 at 14:55