0

I am getting error if i am sharing image to my app from third party.

Like share image from whatsApp, InstagramDownloader, etc.

I am getting uri and changing it to path.

If i am sharing video from gallery it's working fine but, when i am sharing from third party app it's crashing.

  public String getpath(Uri contentUri) {

    Cursor cursor = null;
    try {
        String[] proj = {MediaStore.Video.Media.DATA};

        cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    } finally {
        if (cursor != null) {
            cursor.close();
        }
    }
}

Please if anyone have any suggestion help me..

Drake
  • 1
  • "I am getting uri and changing it to path" -- there is no path. Use the `Uri` directly, such as with `openInputStream()` on a `ContentResolver`. – CommonsWare Apr 19 '20 at 14:40
  • I know to use `InputStream inputStream = getContentResolver().openInputStream(uri);` but then how i can change it to string ... – Drake Apr 20 '20 at 13:10
  • how to get path from InputStream. – Drake Apr 20 '20 at 13:18
  • "how to get path from InputStream" -- **there is no path**. Stop thinking in terms of paths. The stream gives you the content itself, whatever that content is. – CommonsWare Apr 20 '20 at 13:39
  • @CommonsWare AFAIK accessing a file path from the `_data` column is still viable. Some libraries require us to pass a file path instead of an `Uri`. In Android R, file path access is accessible once again and there is no other way to get a file path without using the `_data` column. – HB. Apr 25 '20 at 08:57
  • @Drake If you are still stuck with this, have a look at my library. It works with most known file providers and if a file is selected from an unknown provider, the file will be copied to your applications directory (where the file path is accessible). Note, you should pass a `Uri` returned from `MediaStore`. https://github.com/HBiSoft/PickiT – HB. Apr 25 '20 at 09:08
  • @HB. "AFAIK accessing a file path from the _data column is still viable." -- it was never viable. Most `Uri` values do not support `query()` for `_data`, throwing exceptions if you try. Of those that do, some will return `null`. Of those that return a non-`null` value, some will return filesystem paths that you cannot access (e.g., removable storage) regardless of permissions. "Some libraries require us to pass a file path instead of an `Uri`" -- then don't use them. – CommonsWare Apr 25 '20 at 11:23
  • @CommonsWare "Most `Uri` values do not support `query()` for `_data`, throwing exceptions if you try. Of those that do, some will return `null`. Of those that return a non-`null` value, some will return filesystem paths that you cannot access (e.g., removable storage) regardless of permissions." I have not experienced this thus far. I've tested multiple providers (incl. removable storage), on multiple devices. If you want, you can have a look at the library I've mentioned above. "then don't use them" - FFmpeg is the only reliable way of encoding/decoding videos, and it requires a file object. – HB. Apr 25 '20 at 11:37

0 Answers0