我正在尝试从存储中选择一个视频,当我从图库或存储中选择一个视频时,一切正常,但是无论何时从最近的文件夹中选择一个视频,我都无法获取该视频的路径... < / p>
当我从最近的视频中选择视频时,path始终为null
这是我的代码,用于从uri获取路径并为其创建缩略图...
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
String str =getPath(uri);
Log.i("uri",uri.toString());
Log.i("str",str);
Bitmap bitmap = ThumbnailUtils.createVideoThumbnail(str,MediaStore.Images.Thumbnails.MINI_KIND);
ImageView imageView =findViewById(R.id.thumbnail);
imageView.setImageBitmap(bitmap);
}
}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Video.Media.DATA };
@SuppressLint("Recycle")
Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Video.Media.DATA);
return cursor.getString(column_index);
}
else
return null;
}
它应该适用于我从手机中任何地方选择的视频,
但是它仅在我尝试从图库中获取视频时有效