在我的应用程序中,我启动一个意图来选择这样的视频文件
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("video/*");
startActivityForResult(intent, kVideoPickerRequestCode);
当它回来时,我得到这样的 Uri:
if(resultCode == RESULT_OK) {
if (requestCode == kVideoPickerRequestCode) {
Uri selectedVideoURI = data.getData();
}
}
这个 Uri 似乎适合我的 VideoView。它可以完美地播放所选视频。但现在我想将视频上传到服务器,因此需要一个文件。
无论我如何尝试将 Uri 转换为字符串路径,它都会失败......我读了一些关于此的帖子,例如如何将 content:// URI 转换为实际文件路径? https://stackoverflow.com/questions/7154292/how-to-convert-content-uri-into-actual-file-path
or
尝试从 Android 应用程序将录制的视频上传到服务器时出现 FileNotFoundException https://stackoverflow.com/questions/17558560/filenotfoundexception-when-trying-to-upload-a-recorded-video-to-server-from-andr但这对我不起作用。
像这样的代码总是返回 NULL 作为路径:
public static String getRealPathFromUri(Activity activity, Uri contentUri) {
String[] proj = { MediaStore.Images.Media.DATA };
Cursor cursor = getActivity().getContentResolver().query(uri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
我的测试设备运行的是 Android 4.4.2。难道是 KitKat 存在安全问题吗?
感谢您的任何帮助提示!