来自媒体商店的 Android 视频文件路径为空 [英] Android video file path from media store is coming as null
问题描述
我正在尝试获取视频缩略图的视频文件路径.我不知道为什么我根据这里的一些解决方案修改后它仍然为空.安卓版本是6.0.1.
用户点击浮动操作按钮并召唤视频库.
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.addNote);fab.setOnClickListener(new View.OnClickListener() {@覆盖公共无效onClick(查看视图){意图意图=新意图();intent.setType("video/*");Intent.setAction(Intent.ACTION_GET_CONTENT);startActivityForResult(Intent.createChooser(intent, "Select Video"), REQUEST_TAKE_GALLERY_VIDEO);}});
当用户从图库中选择一个想要的视频时,该视频会转到它会被整理出来的活动.
@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {super.onActivityResult(requestCode, resultCode, data);如果(resultCode == RESULT_OK){uri uri = data.getData();Log.d(TAG, "Uri: " + uri);Log.d(TAG, "Uri 权限:" + 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;//来源不是来自设备捕获或选择如果(光标==空){返回 uri.getPath();} 别的 {cursor.moveToFirst();idx = cursor.getColumnIndex(MediaStore.Video.VideoColumns.DATA);如果(idx == -1){Log.d(TAG, "uri 路径:" + path);返回 uri.getPath();}}字符串路径 = cursor.getString(idx);Log.d(TAG, "路径:" + 路径);游标.关闭();返回路径;}
结果:我得到了空值 (-1) 并得到了 uri 的路径,这不是正确的路径.我需要视频文件的完整路径.
Uri: content://com.android.providers.media.documents/document/video%3A6174Uri 权限:com.android.providers.media.documents文件管理器字符串:/document/video:6174**uri 路径:16842794**selectedImagePath:/document/video:6174
并召唤视频库
不,它没有.它允许用户从任何支持 ACTION_GET_CONTENT
的 video/*
的 MIME 类型的活动中进行选择.您返回的 Uri
可以来自任何东西,不一定是图库"应用程序,也不一定是指向文件的应用程序.Uri
可以指向:
- 外部存储上的文件,您可能可以直接读取
- 可移动存储上的文件,您无法访问
- 某个其他应用程序的内部存储文件
- 数据库中
BLOB
列的内容 - 必须即时解密的东西
- 设备上尚不存在且需要下载的内容
- 等等
获取视频文件路径的方法
可靠地,您可以从 query()
返回的唯一值是 OpenableColumns
,用于内容的大小和显示名称".>
您需要:
使用接受
content
Uri
作为参数的缩略图引擎,或者使用
ContentResolver
和openInputStream()
获取内容的InputStream
,然后使用一些接受InputStream
的缩略图引擎code>InputStream 作为参数,或者使用
ContentResolver
和openInputStream()
获取内容的InputStream
,然后使用该流制作您自己的文件包含内容中字节的副本,因此您可以将自己的文件与一些需要文件的缩略图引擎一起使用,或者不要使用
ACTION_GET_CONTENT
,而是通过向MediaStore
询问所有视频来呈现您自己的选择器"用户界面,因为您可以获得这些视频的缩略图来自MediaStore
(请参阅此示例应用)
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
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, orUse
ContentResolver
andopenInputStream()
to get anInputStream
on the content, then use some thumbnail engine that accepts anInputStream
as a parameter, orUse
ContentResolver
andopenInputStream()
to get anInputStream
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, orDo not use
ACTION_GET_CONTENT
, but instead render your own "chooser" UI by asking theMediaStore
for all videos, as you can get thumbnails of those videos fromMediaStore
(see this sample app)
这篇关于来自媒体商店的 Android 视频文件路径为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!