0

In my app I handle sharing of images/videos from other apps.

The problem is that if I want to handle the images from gallery it works great with following:

    Uri fileUri = (Uri) data.getParcelableExtra(Intent.EXTRA_STREAM);


    String[] projection = {MediaStore.MediaColumns.DATA};
    CursorLoader cursorLoader = new CursorLoader(mChatView.returnContext(), fileUri, projection, null, null, null);
    Cursor cursor = cursorLoader.loadInBackground();
    cursor.moveToFirst();
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
    File selectedImage = new File(cursor.getString(column_index));

I am getting the desired file which I want.

The problem is that is the image is shared via other app (Whatsapp/Fb Messenger) I am getting:

cursor == null

Example of Whatsapp URI:

fileUri = file:///storage/emulated/0/WhatsApp/Media/WhatsApp%20Images/Sent/IMG-20160616-WA0000.jpg

How can I access the file?

-Tested on Nexus 5 - Android v 6.0.1

just_user
  • 11,769
  • 19
  • 90
  • 135
Florin T.
  • 85
  • 1
  • 9

1 Answers1

0

Do it like this :

    String uri = data.getParcelableExtra(Intent.EXTRA_STREAM).toString();

    Uri fileUri = (Uri) data.getParcelableExtra(Intent.EXTRA_STREAM);

    File selectedImage;

    if (uri.startsWith("file://")) {
        selectedImage = new File(fileUri.getPath());
    } 
    else 
    {
        String[] projection = {MediaStore.MediaColumns.DATA};
        CursorLoader cursorLoader = new CursorLoader(mChatView.returnContext(), fileUri, projection, null, null, null);
        Cursor cursor = cursorLoader.loadInBackground();
        cursor.moveToFirst();
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA);
        selectedImage = new File(cursor.getString(column_index));
    }
Florin T.
  • 85
  • 1
  • 9
Janki Gadhiya
  • 4,492
  • 2
  • 29
  • 59
  • I'm having the same problem here, but instead off my uri looks like Florin T. My uri looks like this: content://com.whatsapp.provider.media/item/16348. Any Idea of what can I do? – Eduardo Bonfa Jun 09 '17 at 18:00
  • @EduardoBonfa You can use the same function which I have provided in above code snippet. The else part will take care of your URI. – Janki Gadhiya Jun 15 '17 at 07:20
  • 1
    It'n not working here, please check my question here https://stackoverflow.com/questions/44465601/get-path-from-another-app-whatsapp – Eduardo Bonfa Jun 16 '17 at 12:16