如何从URI获取完整文件path

我想从URI获取完整的文件path。 该URI不是一个图像,但它是一个音乐文件,但如果我像MediaStore解决scheme那样做,如果应用程序用户selectAstro作为浏览器而不是音乐播放器,它将不起作用。 我如何解决这个问题?

使用:

String path = yourAndroidURI.uri.getPath() // "file:///mnt/sdcard/FileName.mp3" File file = new File(new URI(path)); 

要么

 String path = yourAndroidURI.uri.toString() // "/mnt/sdcard/FileName.mp3" File file = new File(new URI(path)); 

尝试这个。

 public String getRealPathFromURI(Uri contentUri) { String[] proj = { MediaStore.Audio.Media.DATA }; Cursor cursor = managedQuery(contentUri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } 

使用PathUtil类从URI获取path(已testing和正在运行)

 String filePath=PathUtil.getPath(context,yourURI); 

PathUtil.java

 import android.annotation.SuppressLint; import android.content.ContentUris; import android.content.Context; import android.database.Cursor; import android.net.Uri; import android.os.Build; import android.os.Environment; import android.provider.DocumentsContract; import android.provider.MediaStore; import java.net.URISyntaxException; /** * Created by Aki on 1/7/2017. */ public class PathUtil { /* * Gets the file path of the given Uri. */ @SuppressLint("NewApi") public static String getPath(Context context, Uri uri) throws URISyntaxException { final boolean needToCheckUri = Build.VERSION.SDK_INT >= 19; String selection = null; String[] selectionArgs = null; // Uri is different in versions after KITKAT (Android 4.4), we need to // deal with different Uris. if (needToCheckUri && DocumentsContract.isDocumentUri(context.getApplicationContext(), uri)) { if (isExternalStorageDocument(uri)) { final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); return Environment.getExternalStorageDirectory() + "/" + split[1]; } else if (isDownloadsDocument(uri)) { final String id = DocumentsContract.getDocumentId(uri); uri = ContentUris.withAppendedId( Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); } else if (isMediaDocument(uri)) { final String docId = DocumentsContract.getDocumentId(uri); final String[] split = docId.split(":"); final String type = split[0]; if ("image".equals(type)) { uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; } else if ("video".equals(type)) { uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; } else if ("audio".equals(type)) { uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; } selection = "_id=?"; selectionArgs = new String[]{ split[1] }; } } if ("content".equalsIgnoreCase(uri.getScheme())) { String[] projection = { MediaStore.Images.Media.DATA }; Cursor cursor = null; try { cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); if (cursor.moveToFirst()) { return cursor.getString(column_index); } } catch (Exception e) { } } else if ("file".equalsIgnoreCase(uri.getScheme())) { return uri.getPath(); } return null; } /** * @param uri The Uri to check. * @return Whether the Uri authority is ExternalStorageProvider. */ public static boolean isExternalStorageDocument(Uri uri) { return "com.android.externalstorage.documents".equals(uri.getAuthority()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is DownloadsProvider. */ public static boolean isDownloadsDocument(Uri uri) { return "com.android.providers.downloads.documents".equals(uri.getAuthority()); } /** * @param uri The Uri to check. * @return Whether the Uri authority is MediaProvider. */ public static boolean isMediaDocument(Uri uri) { return "com.android.providers.media.documents".equals(uri.getAuthority()); } } 

您可以使用从不同SDK版本得到的文件path

使用RealPathUtils

 public class RealPathUtils { @SuppressLint("NewApi") public static String getRealPathFromURI_API19(Context context, Uri uri){ String filePath = ""; String wholeID = DocumentsContract.getDocumentId(uri); // Split at colon, use second item in the array String id = wholeID.split(":")[1]; String[] column = { MediaStore.Images.Media.DATA }; // where id is equal to String sel = MediaStore.Images.Media._ID + "=?"; Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel, new String[]{ id }, null); int columnIndex = cursor.getColumnIndex(column[0]); if (cursor.moveToFirst()) { filePath = cursor.getString(columnIndex); } cursor.close(); return filePath; } @SuppressLint("NewApi") public static String getRealPathFromURI_API11to18(Context context, Uri contentUri) { String[] proj = { MediaStore.Images.Media.DATA }; String result = null; CursorLoader cursorLoader = new CursorLoader( context, contentUri, proj, null, null, null); Cursor cursor = cursorLoader.loadInBackground(); if(cursor != null){ int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); result = cursor.getString(column_index); } return result; } public static String getRealPathFromURI_BelowAPI11(Context context, Uri contentUri){ String[] proj = { MediaStore.Images.Media.DATA }; Cursor cursor = context.getContentResolver().query(contentUri, proj, null, null, null); int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); return cursor.getString(column_index); } } 

**现在从URI获取文件path**

  String path = null; if (Build.VERSION.SDK_INT < 11) path = RealPathUtils.getRealPathFromURI_BelowAPI11(MainActivity.this, uri); // SDK >= 11 && SDK < 19 else if (Build.VERSION.SDK_INT < 19) path = RealPathUtils.getRealPathFromURI_API11to18(MainActivity.this, uri); // SDK > 19 (Android 4.4) else path = RealPathUtils.getRealPathFromURI_API19(MainActivity.this, uri); Log.d(TAG, "File Path: " + path); // Get the file instance File file = new File(path); 
 String uri_path = "file:///mnt/sdcard/FileName.mp3"; File f = new removeUriFromPath(uri_path)); public static String removeUriFromPath(String uri) { return uri.substring(7, uri.length()); }