android中调用系统的图片浏览器后,返回了下面这样的uri----content:mediaexternalimagesmedia2

android中调用系统的图片浏览器后,返回了下面这样的uri----content:mediaexternalimagesmedia2,第1张

从URI获得文件路径

string myImageUrl = "content://media/external/images/media/";Uri uri = Uriparse(myImageUrl); String[] proj = { MediaStoreImagesMediaDATA }; Cursor actualimagecursor = thisctxmanagedQuery(uri,proj,null,null,null); int actual_image_column_index = actualimagecursorgetColumnIndexOrThrow(MediaStoreImagesMediaDATA); actualimagecursormoveToFirst(); String img_path = actualimagecursorgetString(actual_image_column_index);

File file = new File(img_path);Uri fileUri = UrifromFile(file);

/

根据Uri获取绝对路径,解决Android44以上版本Uri转换

@param activity

@param imageUri

@author yaoxing

@date 2014-10-12

/

@TargetApi(19)

public static String getImageAbsolutePath(Activity context, Uri imageUri) {

if (context == null || imageUri == null)

return null;

if (androidosBuildVERSIONSDK_INT >= androidosBuildVERSION_CODESKITKAT && DocumentsContractisDocumentUri(context, imageUri)) {

if (isExternalStorageDocument(imageUri)) {

String docId = DocumentsContractgetDocumentId(imageUri);

String[] split = docIdsplit(":");

String type = split[0];

if ("primary"equalsIgnoreCase(type)) {

return EnvironmentgetExternalStorageDirectory() + "/" + split[1];

}

} else if (isDownloadsDocument(imageUri)) {

String id = DocumentsContractgetDocumentId(imageUri);

Uri contentUri = ContentUriswithAppendedId(Uriparse("content://downloads/public_downloads"), LongvalueOf(id));

return getDataColumn(context, contentUri, null, null);

} else if (isMediaDocument(imageUri)) {

String docId = DocumentsContractgetDocumentId(imageUri);

String[] split = docIdsplit(":");

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;

}

String selection = MediaStoreImagesMedia_ID + "=";

String[] selectionArgs = new String[] { split[1] };

return getDataColumn(context, contentUri, selection, selectionArgs);

}

} // MediaStore (and general)

else if ("content"equalsIgnoreCase(imageUrigetScheme())) {

// Return the remote address

if (isGooglePhotosUri(imageUri))

return imageUrigetLastPathSegment();

return getDataColumn(context, imageUri, null, null);

}

// File

else if ("file"equalsIgnoreCase(imageUrigetScheme())) {

return imageUrigetPath();

}

return null;

}

public static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {

Cursor cursor = null;

String column = MediaStoreImagesMediaDATA;

String[] projection = { column };

try {

cursor = contextgetContentResolver()query(uri, projection, selection, selectionArgs, null);

if (cursor != null && cursormoveToFirst()) {

int index = cursorgetColumnIndexOrThrow(column);

return cursorgetString(index);

}

} finally {

if (cursor != null)

cursorclose();

}

return null;

}

/

@param uri The Uri to check

@return Whether the Uri authority is ExternalStorageProvider

/

public static boolean isExternalStorageDocument(Uri uri) {

return "comandroidexternalstoragedocuments"equals(urigetAuthority());

}

/

@param uri The Uri to check

@return Whether the Uri authority is DownloadsProvider

/

public static boolean isDownloadsDocument(Uri uri) {

return "comandroidprovidersdownloadsdocuments"equals(urigetAuthority());

}

/

@param uri The Uri to check

@return Whether the Uri authority is MediaProvider

/

public static boolean isMediaDocument(Uri uri) {

return "comandroidprovidersmediadocuments"equals(urigetAuthority());

}

/

@param uri The Uri to check

@return Whether the Uri authority is Google Photos

/

public static boolean isGooglePhotosUri(Uri uri) {

return "comgoogleandroidappsphotoscontent"equals(urigetAuthority());

}

最近做项目要通过的绝对路径找到的URI,然后删除,小小总结一下获取URI的方法,亲自试验在

android 413的系统上都是可用的。

1将所有的路径取出,遍历比较找到需要的路径,取出URI,效率较低

其中 MediaStoreMediaColumnsDATA 字段存的就是的绝对路径,

最后mImageUri得到的就是的URI

1 Uri mUri = Uriparse("content://media/external/images/media");

2 Uri mImageUri = null;

3 Cursor cursor = managedQuery(

4 MediaStoreImagesMediaEXTERNAL_CONTENT_URI, null, null, null,

5 MediaStoreImagesMediaDEFAULT_SORT_ORDER);

6 cursormoveToFirst();

7

8 while (!cursorisAfterLast()) {

9 String data = cursorgetString(cursor

10 getColumnIndex(MediaStoreMediaColumnsDATA));

11 if (picPathequals(data)) {

12 int ringtoneID = cursorgetInt(cursor

13 getColumnIndex(MediaStoreMediaColumns_ID));

14 mImageUri = UriwithAppendedPath(mUri, "" + ringtoneID);

15 break;

16 }

17 cursormoveToNext();

18 }

2直接从媒体数据库根据字段取出相应的记录,效率较高

1 //TYLT: add by duanyf 20121027 start

2 String type = UtilsensureNotNull(intentgetType());

3 Logd(TAG, "uri is " + uri);

4 if (urigetScheme()equals("file") && (typecontains("image/")))

{

5 String path = urigetEncodedPath();

6 Logd(TAG, "path1 is " + path);

7 if (path != null) {

8 path = Uridecode(path);

9 Logd(TAG, "path2 is " + path);

10 ContentResolver cr = thisgetContentResolver();

11 StringBuffer buff = new StringBuffer();

12 buffappend("(")

13 append(ImagesImageColumnsDATA)

14 append("=")

15 append("'" + path + "'")

16 append(")");

17 Cursor cur = crquery(

18 ImagesMediaEXTERNAL_CONTENT_URI,

19 new String[] { ImagesImageColumns_ID },

20 bufftoString(), null, null);

21 int index = 0;

22 for (curmoveToFirst(); !curisAfterLast(); cur

23 moveToNext()) {

24 index = curgetColumnIndex(ImagesImageColumns_ID);

25 // set _id value

26 index = curgetInt(index);

27 }

28 if (index == 0) {

29 //do nothing

30 } else {

31 Uri uri_temp = Uri

32 parse("content://media/external/images/media/"

33 + index);

34 Logd(TAG, "uri_temp is " + uri_temp);

35 if (uri_temp != null) {

36 uri = uri_temp;

37 }

38 }

39 }

40 }

41 //TYLT: add by duanyf 20121027 end

3直接根据路径通过 ContentProvider 的 delete() 方法删除,两行代码搞定,效率最高

1 String params[] = new String[]{filepath};

2

ctxgetContentResolver()delete(MediaStoreImagesMediaEXTERNAL_CONTENT_URI,

MediaStoreImagesMediaDATA + " LIKE ", params);

Android调用系统拍照的代码:

创建Intent Intent openCameraIntent = new Intent(MediaStoreACTION_IMAGE_CAPTURE);

String imagePath = EnvironmentgetExternalStorageDirectory() + "/image" ;

设置文件路径 File file = new File(imagePath);

if(!fileexists()){

filemkdirs(); }

设置URI,指定相册拍照后保存的路径,Uri imageUri = UrifromFile(new File(imagePath, "imagejsp"));

openCameraIntentputExtra(MediaStoreEXTRA_OUTPUT, imageUri);

startActivityForResult(openCameraIntent, REQUEST_CODE_TAKING_PICTURES);

拍照完以后,文件就会保存在这个指定的目录下了。Uri 里指定了相机拍照的路径。

以上就是关于android中调用系统的图片浏览器后,返回了下面这样的uri----content://media/external/images/media/2全部的内容,包括:android中调用系统的图片浏览器后,返回了下面这样的uri----content://media/external/images/media/2、android4.4版本uri转换怎么调用、android如何通过path得到uri等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/9282732.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-26
下一篇 2023-04-26

发表评论

登录后才能评论

评论列表(0条)

保存