问题
In my app I start an Intent to pick a video file like this
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("video/*");
startActivityForResult(intent, kVideoPickerRequestCode);
When it comes back i get the Uri like this:
if(resultCode == RESULT_OK) {
if (requestCode == kVideoPickerRequestCode) {
Uri selectedVideoURI = data.getData();
}
}
This Uri seems to be fine for my VideoView. It plays back the picked video perfectly. But now I'd like to upload the video to a server and therefore need a File.
No matter how i try to convert the Uri to a String-Path, it fails... I read some SO posts on this like How to convert content:// Uri into actual file path? or FileNotFoundException when trying to upload a recorded video to server from Android app but it's just not working for me.
Code like this always returns NULL as the path:
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);
}
My test-device is running Android 4.4.2. Can it be that it's some security issue with KitKat?
Thanks for any helping hint!
回答1:
Your Uri does not necessarily represent a file on the disk. It begins with content:// meaning it is served up by the content provider for that type of file.
You don't actually need to use a file in the filesystem here (although, you can). What you want to do is stream the data from the content provider directly to the server.
There are some good examples - I would try this one for how to do this, in the first section "How to access binary data of existing content providers"
回答2:
Hello i know it is too late but recently i found the solution of same problem
I was also facing the same problem but here is the method
public static String getPath (final Context context, final Uri uri) {
if (DEBUG) Log.d(TAG + " File -", "Authority: " + uri.getAuthority() + ", Fragment: " + uri.getFragment() + ", Port: " + uri.getPort() + ", Query: " + uri.getQuery() + ", Scheme: " + uri.getScheme() + ", Host: " + uri.getHost() + ", Segments: " + uri.getPathSegments().toString() ); final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; // DocumentProvider if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) { // ExternalStorageProvider else if (isExternalStorageDocument(uri)) { final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; if ("primary".equalsIgnoreCase(type)) { return Environment.getExternalStorageDirectory() + "/" + split[1]; } // TODO handle non-primary volumes } // MediaProvider else if (isMediaDocument(uri)) { final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; Uri contentUri = null; if ("image".equals(type)) { contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; } else if ("video".equals(type)) { contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; } else if ("audio".equals(type)) { contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; } final String selection = "_id=?"; final String[] selectionArgs = new String[] { split[1] }; return getDataColumn(context, contentUri, selection, selectionArgs); } } // MediaStore (and general) else if ("content".equalsIgnoreCase(uri.getScheme())) { // Return the remote address if (isGooglePhotosUri(uri)) return uri.getLastPathSegment(); return getDataColumn(context, uri, null, null); } // File else if ("file".equalsIgnoreCase(uri.getScheme())) { return uri.getPath(); } return null; }
which i get from this url
--> other methods can be fetched from this FILE UTILS file enjoy coding:) --> and it gets me full video path from the uri hope it helps you :)
来源:https://stackoverflow.com/questions/21186016/get-file-path-from-uri-from-video-chooser