0

While parsing the results of Intent.ACTION_GET_CONTENT:

Intent intent = new Intent();
        intent.setType("video/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(
                Intent.createChooser(intent, "Select Video"), READ_REQUEST_CODE);

On SDK 27 I receive :

dat=content://com.android.providers.media.documents/document/video:14691

on SDK 23 :

dat=content://com.android.externalstorage.documents/document/primary:some_dir/VID-20171013-WA0010_repaired.mp4 

I'm using RealPathUtils.class to get the absolute path of the file. It does the job on most of the devices tested, but it failed on SDK 25.

RealPathUtils :

public class RealPathUtils {

    @SuppressLint("NewApi")
    public static String getRealPathFromURI_API19(Context context, Uri uri){
        String filePath = "";
        String wholeID = DocumentsContract.getDocumentId(uri);

        // Split at colon, use second item in the array
        String id = wholeID.split(":")[1];

        String[] column = { MediaStore.Video.Media.DATA };

        // where id is equal to
        String sel = MediaStore.Video.Media._ID + "=?";

        Cursor cursor = context.getContentResolver().query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                column, sel, new String[]{ id.replaceAll("(?i)^.*/", "") }, null);

        int columnIndex = cursor.getColumnIndex(column[0]);

        if (cursor.moveToFirst()) {
            filePath = cursor.getString(columnIndex);
        }
        cursor.close();
        return filePath;
    }


    @SuppressLint("NewApi")
    public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) {
        String[] proj = { MediaStore.Video.Media.DATA };
        String result = null;

        CursorLoader cursorLoader = new CursorLoader(
                context,
                contentUri, proj, null, null, null);
        Cursor cursor = cursorLoader.loadInBackground();

        if(cursor != null){
            int column_index =
                    cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
            cursor.moveToFirst();
            result = cursor.getString(column_index);
        }
        return result;
    }

}

I'm doubtful about using RealPathUtils on a production environment without knowing if it will fail on untested scenarios.

  1. Is there a more generic/reliable way of retrieving the absolute path of a file on SDK > 15 devices?
  2. Would a build-in file explorer be a solution?
Pedro Lobito
  • 85,689
  • 29
  • 230
  • 253
  • 1
    There is no requirement that a content:// URI points to a file, much less a file that your app has access to. – ianhanniballake Nov 16 '18 at 01:12
  • See also [this](https://stackoverflow.com/questions/49221312/android-get-real-path-of-a-txt-file-selected-from-the-file-explorer), [this](https://stackoverflow.com/questions/48510584/onactivityresults-intent-getpath-doesnt-give-me-the-correct-filename), and [this](https://stackoverflow.com/questions/35870825/getting-the-absolute-file-path-from-content-uri-for-searched-images). – CommonsWare Nov 16 '18 at 12:04
  • @ianhanniballake Sure, but if it does, how to get it? The question was marked has duplicated, but the accepted answer ( from CommonsWare) has 0 votes. My question remains, **if the file exists on disk, how to get its absolute path?** – Pedro Lobito Nov 16 '18 at 12:12
  • You can't do anything with the absolute path, so stop trying. – ianhanniballake Nov 16 '18 at 19:11
  • @ianhanniballake I cannot use `content://` as argument to `ffmpeg`. – Pedro Lobito Nov 16 '18 at 19:41
  • If you can only work with files, stop using `Intent` actions like `ACTION_GET_CONTENT`. Integrate [a file-chooser library](https://android-arsenal.com/tag/35?sort=created), which will limit you to files that your app can access on the filesystem. – CommonsWare Nov 16 '18 at 20:01
  • Ok, that sounds like the "build-in file explorer" I mentioned in my question. [file-chooser library](https://android-arsenal.com/tag/35?sort=created) seems promising, thank you. – Pedro Lobito Nov 16 '18 at 20:10

0 Answers0