我正在尝试获取视频缩略图的视频文件路径。我不确定为什么在我根据此处的一些解决方案进行修改后它仍然为空。安卓版本为6.0.1。
用户单击 float 操作按钮并召唤视频库。
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);
}
});
当用户从图库中选择所需的视频时,该视频将转到将被分类的 Activity 。
@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);
}
}
获取视频文件路径的方法。
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;
}
结果:我得到了 null (-1) 并得到了 uri 的路径,这不是正确的路径。我需要视频文件的完整路径。
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
最佳答案
and summons a gallery of videos
不,不是的。它允许用户从支持
ACTION_GET_CONTENT
的任何 Activity 中进行选择。对于 video/*
的 MIME 类型. Uri
你回来的可以来自任何东西,不一定是“图库”应用程序,也不一定是指向文件的应用程序。 Uri
可以指向:BLOB
的内容数据库中的列The method to get the path of the video file
您可以从中获得的唯一值
query()
,可靠的是 OpenableColumns
, 用于内容的大小和“显示名称”。您需要:
content
的缩略图引擎Uri
作为参数,或 ContentResolver
和 openInputStream()
获取 InputStream
在内容上,然后使用一些接受 InputStream
的缩略图引擎作为参数,或 ContentResolver
和 openInputStream()
获取 InputStream
在内容上,然后使用该流制作您自己的文件,其中包含内容中字节的副本,这样您就可以将自己的文件与一些需要文件的缩略图引擎一起使用,或 ACTION_GET_CONTENT
,而是通过询问 MediaStore
来呈现您自己的“选择器”UI对于所有视频,因为您可以从 MediaStore
获取这些视频的缩略图(见 this sample app ) 关于来自媒体商店的 Android 视频文件路径为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48559256/