Albert
Albert

Reputation: 63

How to get video File Path from uri

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)

Upvotes: 4

Views: 5690

Answers (1)

Hardy
Hardy

Reputation: 2606

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 :)

Upvotes: 4

Related Questions