Android video file path from media store is coming

2019-03-06 06:33发布

I am trying to get the path of the video file for a video thumbnail. I'm not sure why it is still coming as null after I modified based on some solutions here. The version of android is 6.0.1.

The user clicks the floating action button and summons a gallery of videos.

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.addNote);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent();
            intent.setType("video/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent, "Select Video"), REQUEST_TAKE_GALLERY_VIDEO);
        }
    });

When the user selects a desired video from the gallery, the video goes to the activity which it'll be sorted out.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK) {

        Uri uri = data.getData();
        Log.d(TAG, "Uri: " + uri);

        Log.d(TAG, "Uri authority: " + uri.getAuthority());

        String filemanagerstring = uri.getPath();
        Log.d(TAG, "filemanagerstring: " + filemanagerstring);

        String selectedImagePath = getPath(uri);
        Log.d(TAG, "selectedImagePath: " + selectedImagePath);
    }
}

The method to get the path of the video file.

public String getPath(Uri uri) {

    Cursor cursor = this.getContentResolver().query(uri, null, null, null, null);
    int idx = 0;

    //Source not from device capture or selection
    if (cursor == null) {
        return uri.getPath();
    } else {
        cursor.moveToFirst();
        idx = cursor.getColumnIndex(MediaStore.Video.VideoColumns.DATA);
        if (idx == -1) {
            Log.d(TAG, "uri path: " + path);
            return uri.getPath();
        }
    }
    String path =  cursor.getString(idx);
    Log.d(TAG, "path: " + path);
    cursor.close();
    return path;
}

Results: I got the null (-1) and got the uri's path, that's not the correct path. I need the full path of the video file.

Uri: content://com.android.providers.media.documents/document/video%3A6174
Uri authority: com.android.providers.media.documents
filemanagerstring: /document/video:6174
**uri path: 16842794**
selectedImagePath: /document/video:6174

1条回答
Deceive 欺骗
2楼-- · 2019-03-06 07:15

and summons a gallery of videos

No, it does not. It allows the user to choose from any activity that supports ACTION_GET_CONTENT for a MIME type of video/*. The Uri that you get back can be from anything, not necessarily a "gallery" app, and not necessarily one that points to a file. The Uri could point to:

  • A file on external storage, one that you might be able to read directly
  • A file on removable storage, which you cannot access
  • A file on internal storage of some other app
  • The contents of a BLOB column in a database
  • Something that has to be decrypted on the fly
  • Something that does not yet exist on the device and needs to be downloaded
  • And so on

The method to get the path of the video file

The only values you can get back from that query(), reliably, are the OpenableColumns, for the size and "display name" of the content.

You need to either:

  • Use a thumbnail engine that accepts a content Uri as a parameter, or

  • Use ContentResolver and openInputStream() to get an InputStream on the content, then use some thumbnail engine that accepts an InputStream as a parameter, or

  • Use ContentResolver and openInputStream() to get an InputStream on the content, then use that stream to make your own file that contains a copy of the bytes from the content, so you can use your own file with some thumbnail engine that requires a file, or

  • Do not use ACTION_GET_CONTENT, but instead render your own "chooser" UI by asking the MediaStore for all videos, as you can get thumbnails of those videos from MediaStore (see this sample app)

查看更多
登录 后发表回答