4

I am trying to get file path from my video uri.

I have gone through with following links but still facing this issue:

get-filename-and-path-from-uri-from-mediastore

get-file-path-from-uri-from-video-chooser

Following is my code:

        Uri imageUri = data.getData();
        String[] proj = { MediaStore.Audio.Media.DATA };
        Cursor cursor = PreferenceHelper.getContext().getContentResolver().query(imageUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
        cursor.moveToFirst();
        String filePath = cursor.getString(column_index);
        sendVideo(activity, filePath, windowId, isChatroom);

content://com.android.providers.media.documents/document/video%3A1142

But always get file path null.

Following is my logs:

10-22 13:09:44.189 21604-21604/com.testapp.chat W/System.err: java.lang.NullPointerException
10-22 13:09:44.190 21604-21604/com.testapp.chat W/System.err:     at java.io.File.fixSlashes(File.java:185)
10-22 13:09:44.191 21604-21604/com.testapp.chat W/System.err:     at java.io.File.<init>(File.java:134)
10-22 13:09:44.191 21604-21604/com.testapp.chat W/System.err:     at com.inscripts.plugins.VideoSharing.sendVideo(VideoSharing.java:125)
10-22 13:09:44.191 21604-21604/com.testapp.chat W/System.err:     at com.inscripts.plugins.VideoSharing.sendVideo(VideoSharing.java:219)
10-22 13:09:44.192 21604-21604/com.testapp.chat W/System.err:     at com.inscripts.plugins.VideoSharing.sendVideoOneOnOne(VideoSharing.java:104)
10-22 13:09:44.193 21604-21604/com.testapp.chat W/System.err:     at com.inscripts.activities.SingleChatActivity.onButtonClick(SingleChatActivity.java:617)
10-22 13:09:44.193 21604-21604/com.testapp.chat W/System.err:     at com.inscripts.custom.CustomAlertDialogHelper.onClick(CustomAlertDialogHelper.java:65)
10-22 13:09:44.193 21604-21604/com.testapp.chat W/System.err:     at android.view.View.performClick(View.java:4456)
10-22 13:09:44.194 21604-21604/com.testapp.chat W/System.err:     at android.view.View$PerformClick.run(View.java:18465)
General Grievance
  • 4,555
  • 31
  • 31
  • 45
Albert
  • 63
  • 1
  • 7

1 Answers1

4

Hello i was also facing the same problem but here is the method

 public static String getPath(final Context context, final Uri uri) {

    if (DEBUG)
        Log.d(TAG + " File -",
                "Authority: " + uri.getAuthority() +
                        ", Fragment: " + uri.getFragment() +
                        ", Port: " + uri.getPort() +
                        ", Query: " + uri.getQuery() +
                        ", Scheme: " + uri.getScheme() +
                        ", Host: " + uri.getHost() +
                        ", Segments: " + uri.getPathSegments().toString()
                );

    final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;

    // DocumentProvider
    if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {

        // ExternalStorageProvider
        else if (isExternalStorageDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            if ("primary".equalsIgnoreCase(type)) {
                return Environment.getExternalStorageDirectory() + "/" + split[1];
            }

            // TODO handle non-primary volumes
        }

        // MediaProvider
        else if (isMediaDocument(uri)) {
            final String docId = DocumentsContract.getDocumentId(uri);
            final String[] split = docId.split(":");
            final String type = split[0];

            Uri contentUri = null;
            if ("image".equals(type)) {
                contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
            } else if ("video".equals(type)) {
                contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
            } else if ("audio".equals(type)) {
                contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
            }

            final String selection = "_id=?";
            final String[] selectionArgs = new String[] {
                    split[1]
            };

            return getDataColumn(context, contentUri, selection, selectionArgs);
        }
    }
    // MediaStore (and general)
    else if ("content".equalsIgnoreCase(uri.getScheme())) {

        // Return the remote address
        if (isGooglePhotosUri(uri))
            return uri.getLastPathSegment();

        return getDataColumn(context, uri, null, null);
    }
    // File
    else if ("file".equalsIgnoreCase(uri.getScheme())) {
        return uri.getPath();
    }

    return null;
}

which i get from this url

--> other methods can be fetched from this FILE UTILS file enjoy coding:) --> and it gets me full video path from the uri hope it helps you :)

Hardy
  • 2,576
  • 1
  • 23
  • 45
  • This is awesome! I probably tried suggestions from 10 different post before finding your answer. Would give it +10 if I could. – wildcat12 May 08 '17 at 15:55
  • 2
    This code is awful. It will not handle arbitrary `Uri` values, will not work in places where device manufacturers change these providers, and makes assumptions about the internal structure of `Uri` values. It specifically will fail for anything served from `FileProvider` and all Android 10+ devices. – CommonsWare Oct 15 '19 at 23:45
  • @CommonsWare do you have the answer? – sak Jul 06 '20 at 14:40
  • @sak: Use the `Uri` itself. For example, use `ContentProvider` and `openInputStream()` to read in the content identified by the `Uri`. – CommonsWare Jul 06 '20 at 17:25