Android如何通过URI获取文件路径示例代码

前言

最近在工作的过程中,遇到不同 Android 版本下 URI 采用不同方式来获取文件路径的问题。

因为需求的原因,要求拍照上传或者从相册中选择图片上传,而且图片是需要经过压缩的,大小不能超过2M。

很快,拍照的这部分就搞定了。那么相册中选择图片的也是一样的道理,应该也是轻松解决了。

至于选择图片的代码,如下所示:

intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.addCategory(Intent.CATEGORY_OPENABLE);
intent.setType("image/*");
startActivityForResult(intent, FILE_CHOOSER_RESULT_CODE);

之后就是在 onActivityResult(int requestCode, int resultCode, Intent data) 中获取到 URI 。

最关键的来了,如果通过 URI 来获取文件呢?

比如,现在 URI 为 content://media/extenral/images/media/17766 ,而我们需要得到对应的文件路径。

if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
 Cursor cursor = context.getContentResolver().query(uri, new String[]{MediaStore.Images.Media.DATA}, null, null, null);
 if (cursor != null) {
  if (cursor.moveToFirst()) {
   int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
   if (columnIndex > -1) {
    path = cursor.getString(columnIndex);
   }
  }
  cursor.close();
 }
 return path;
}

原以为万事大吉,但是在 Android 4.4 及以上的手机上一试,发现根本不行。因为在 Android 4.4 及以上的手机上,获取到的 URI 变成了 content://com.android.providers.media.documents/document/image%3A235700 ,和之前我们预期的不是同一种类型。

这是因为在 Android 4.4 及以上的机型,使用了 DocumentUri 来代表获取到文件的 URI 。

所以我们又要对于 DocumentUri 进行适配。

if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme())) {
  if (DocumentsContract.isDocumentUri(context, uri)) {
   if (isExternalStorageDocument(uri)) {
    // ExternalStorageProvider
    final String docId = DocumentsContract.getDocumentId(uri);
    final String[] split = docId.split(":");
    final String type = split[0];
    if ("primary".equalsIgnoreCase(type)) {
     path = Environment.getExternalStorageDirectory() + "/" + split[1];
     return path;
    }
   } else if (isDownloadsDocument(uri)) {
    // DownloadsProvider
    final String id = DocumentsContract.getDocumentId(uri);
    final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
      Long.valueOf(id));
    path = getDataColumn(context, contentUri, null, null);
    return path;
   } else if (isMediaDocument(uri)) {
    // MediaProvider
    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]};
    path = getDataColumn(context, contentUri, selection, selectionArgs);
    return path;
   }
  }
 }
 private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
  Cursor cursor = null;
  final String column = "_data";
  final String[] projection = {column};
  try {
   cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
   if (cursor != null && cursor.moveToFirst()) {
    final int column_index = cursor.getColumnIndexOrThrow(column);
    return cursor.getString(column_index);
   }
  } finally {
   if (cursor != null)
    cursor.close();
  }
  return null;
 }
 private static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
 }
 private static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
 }
 private static boolean isMediaDocument(Uri uri) { return "com.android.providers.media.documents".equals(uri.getAuthority());
 }

好了,上面的代码还是容易看懂的。这下就解决了对于 Android 4.4 及以上的机型适配。顺便把以 file:// 开头的 URI 适配也补上:

if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
 path = uri.getPath();
 return path;
}

完美了,下面就贴出完整的 FileUtils 代码,拿去用吧:

public final class FileUtils {
 public static String getFilePathByUri(Context context, Uri uri) {
  String path = null;
  // 以 file:// 开头的
  if (ContentResolver.SCHEME_FILE.equals(uri.getScheme())) {
   path = uri.getPath();
   return path;
  }
  // 以 content:// 开头的,比如 content://media/extenral/images/media/17766
  if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme()) && Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
   Cursor cursor = context.getContentResolver().query(uri, new String[]{MediaStore.Images.Media.DATA}, null, null, null);
   if (cursor != null) {
    if (cursor.moveToFirst()) {
     int columnIndex = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
     if (columnIndex > -1) {
      path = cursor.getString(columnIndex);
     }
    }
    cursor.close();
   }
   return path;
  }
  // 4.4及之后的 是以 content:// 开头的,比如 content://com.android.providers.media.documents/document/image%3A235700
  if (ContentResolver.SCHEME_CONTENT.equals(uri.getScheme()) && Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
   if (DocumentsContract.isDocumentUri(context, uri)) {
    if (isExternalStorageDocument(uri)) {
     // ExternalStorageProvider
     final String docId = DocumentsContract.getDocumentId(uri);
     final String[] split = docId.split(":");
     final String type = split[0];
     if ("primary".equalsIgnoreCase(type)) {
      path = Environment.getExternalStorageDirectory() + "/" + split[1];
      return path;
     }
    } else if (isDownloadsDocument(uri)) {
     // DownloadsProvider
     final String id = DocumentsContract.getDocumentId(uri);
     final Uri contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
       Long.valueOf(id));
     path = getDataColumn(context, contentUri, null, null);
     return path;
    } else if (isMediaDocument(uri)) {
     // MediaProvider
     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]};
     path = getDataColumn(context, contentUri, selection, selectionArgs);
     return path;
    }
   }
  }
  return null;
 }
 private static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
  Cursor cursor = null;
  final String column = "_data";
  final String[] projection = {column};
  try {
   cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
   if (cursor != null && cursor.moveToFirst()) {
    final int column_index = cursor.getColumnIndexOrThrow(column);
    return cursor.getString(column_index);
   }
  } finally {
   if (cursor != null)
    cursor.close();
  }
  return null;
 }
 private static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority());
 }
 private static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority());
 }
 private static boolean isMediaDocument(Uri uri) {  return "com.android.providers.media.documents".equals(uri.getAuthority());
 }
}

讲完了,讲完了。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,如果有疑问大家可以留言交流,谢谢大家对脚本之家的支持。

你可能感兴趣的:(Android如何通过URI获取文件路径示例代码)