Android获取本地图片之ACTION_GET_CONTENT与ACTION_PICK区别
(2023-06-20 11:51:39)分类: androidios |
我们都知道下面两种方法都可以打开Android本地图库:
Intent.ACTION_GET_CONTENT
Intent
intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image
@TargetApi(Build.VERSION_CODES.KITKAT)
public
static String getPathFromUriOnKitKat(Activity act, Uri uri) {
String path = null;
if (DocumentsContract.isDocumentUri(act, uri)) {
// 如果是document类型的Uri,则通过document id处理
String docId = DocumentsContract.getDocumentId(uri);
if
("com.android.providers.media.documents".equals(uri.getAuthority()))
{
String id = docId.split(":")[1]; // 解析出数字格式的id
String selection = MediaStore.Images.Media._ID + "=" + id;
path = getPathFromUri(act,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection);
} else if
("com.android.providers.downloads.documents".equals(uri.getAuthority()))
{
Uri contentUri =
ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"),
Long.valueOf(docId));
path = getPathFromUri(act, contentUri, null);
}
} else if ("content".equalsIgnoreCase(uri.getScheme())) {
// 如果是content类型的Uri,则使用普通方式处理
path = getPathFromUri(act, uri, null);
} else if ("file".equalsIgnoreCase(uri.getScheme())) {
// 如果是file类型的Uri,直接获取图片路径即可
path = uri.getPath();
}
return path;
}
1
2