我得到以下类型的URI的是:
/mnt/sdcard/Pictures/WW/ww_1360248819300.jpg
如何上述类型的URI转换为以下网址:
内容://媒体/外部/图像/媒体/ 12
请帮忙谢谢
解决方案 公共静态乌里getImageContentUri(上下文的背景下,文件镜像文件){ 串文件路径= imageFile.getAbsolutePath(); 光标光标= context.getContentResolver()查询( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, 新的String [] {} MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA +=?, 新的String [] {}文件路径,NULL); 如果(光标=空&放大器;!&放大器; cursor.moveToFirst()){ INT ID = cursor.getInt(光标 .getColumnIndex(MediaStore.MediaColumns._ID)); 乌里基本URI = Uri.parse(内容://媒体/外部/图像/媒体); 返回Uri.withAppendedPath(基本URI,+ ID); }其他{ 如果(imageFile.exists()){ ContentValues值=新ContentValues(); values.put(MediaStore.Images.Media.DATA,文件路径); 返回context.getContentResolver()。插入( MediaStore.Images.Media.EXTERNAL_CONTENT_URI,价值观); }其他{ 返回null; } }}
i am getting below type of uri:
/mnt/sdcard/Pictures/WW/ww_1360248819300.jpg
how to convert above type of URI to below URI:
content://media/external/images/media/12
please help Thanks
解决方案public static Uri getImageContentUri(Context context, File imageFile) {
String filePath = imageFile.getAbsolutePath();
Cursor cursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
new String[] { MediaStore.Images.Media._ID },
MediaStore.Images.Media.DATA + "=? ",
new String[] { filePath }, null);
if (cursor != null && cursor.moveToFirst()) {
int id = cursor.getInt(cursor
.getColumnIndex(MediaStore.MediaColumns._ID));
Uri baseUri = Uri.parse("content://media/external/images/media");
return Uri.withAppendedPath(baseUri, "" + id);
} else {
if (imageFile.exists()) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DATA, filePath);
return context.getContentResolver().insert(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
} else {
return null;
}
}
}