直接调用文件管理器选择即可。
1、调用系统提供的选择器,代码如下:
//注意,在Android44系统下建议使用 IntentACTION_OPEN_DOCUMENT方式
if (UtilityisKK()) {
Intent intent = new Intent(IntentACTION_OPEN_DOCUMENT);
intentaddCategory(IntentCATEGORY_OPENABLE);
intentsetType("image
public static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
处理返回结果:
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
superonActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_OK) {
switch (requestCode) {
case PIC_RESULT://选择图库
case PIC_RESULT_KK:
imageFileUri = intentgetData();//获取选择的URI
break;
}
}
2、除此自外,系统还提供一种选择器,这个选择器可以屏蔽掉那个auto backup的目录所以就开始打算用这个选择器来选了
Intent intent=new Intent(IntentACTION_GET_CONTENT);//ACTION_OPEN_DOCUMENT
intentaddCategory(IntentCATEGORY_OPENABLE);
intentsetType("image/jpeg");
if(androidosBuildVERSIONSDK_INT>=androidosBuildVERSION_CODESKITKAT){
startActivityForResult(intent, SELECT_PIC_KITKAT);
}else{
startActivityForResult(intent, SELECT_PIC);
}
为什么要分开不同版本呢其实在43或以下可以直接用ACTION_GET_CONTENT的,在44或以上,官方建议用ACTION_OPEN_DOCUMENT,主要区别是他们返回的Uri43返回的是带文件路径的,而44返回的却是content://comandroidprovidersmediadocuments/document/image:3951这样的,没有路径,只有编号的uri可以通过以下方式,处理URI。
参考:Android 44从图库选择,获取路径并裁剪
public static String getPath(final Context context, final Uri uri) {
final boolean isKitKat = BuildVERSIONSDK_INT >= BuildVERSION_CODESKITKAT;
// DocumentProvider
if (isKitKat && DocumentsContractisDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContractgetDocumentId(uri);
final String[] split = docIdsplit(":");
final String type = split[0];
if ("primary"equalsIgnoreCase(type)) {
return EnvironmentgetExternalStorageDirectory() + "/" + split[1];
}
// TODO handle non-primary volumes
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContractgetDocumentId(uri);
final Uri contentUri = ContentUriswithAppendedId(
Uriparse("content://downloads/public_downloads"), LongvalueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContractgetDocumentId(uri);
final String[] split = docIdsplit(":");
final String type = split[0];
Uri contentUri = null;
if ("image"equals(type)) {
contentUri = MediaStoreImagesMediaEXTERNAL_CONTENT_URI;
} else if ("video"equals(type)) {
contentUri = MediaStoreVideoMediaEXTERNAL_CONTENT_URI;
} else if ("audio"equals(type)) {
contentUri = MediaStoreAudioMediaEXTERNAL_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(urigetScheme())) {
// Return the remote address
if (isGooglePhotosUri(uri))
return urigetLastPathSegment();
return getDataColumn(context, uri, null, null);
}
// File
else if ("file"equalsIgnoreCase(urigetScheme())) {
return urigetPath();
}
return null;
}
public static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = contextgetContentResolver()query(uri, projection, selection, selectionArgs,
null);
if (cursor != null && cursormoveToFirst()) {
final int index = cursorgetColumnIndexOrThrow(column);
return cursorgetString(index);
}
} finally {
if (cursor != null)
cursorclose();
}
return null;
}
public static boolean isExternalStorageDocument(Uri uri) {
return "comandroidexternalstoragedocuments"equals(urigetAuthority());
}
public static boolean isDownloadsDocument(Uri uri) {
return "comandroidprovidersdownloadsdocuments"equals(urigetAuthority());
}
public static boolean isMediaDocument(Uri uri) {
return "comandroidprovidersmediadocuments"equals(urigetAuthority());
}
public static boolean isGooglePhotosUri(Uri uri) {
return "comgoogleandroidappsphotoscontent"equals(urigetAuthority());
}
3、使用其它开源组件如PhotoView。
以上就是关于android4.4之后怎么根据sd卡中的图片路径path获取图片对应的Uri地址全部的内容,包括:android4.4之后怎么根据sd卡中的图片路径path获取图片对应的Uri地址、、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)