android 缩略图uri_Android 文件绝对路径和Content开头的Uri互相转换

工作中遇到的问题。拍照获取图片后是得到的路径是

file:///storage/emulated/0/Android/data/com.zehin.mingchuliangzao3/cache/PostPicture/20160905182015.jpg

但是我想要的路径是:

content://media/external/images/media/212304

这种 Uri类型的

查阅资料找到如下方法

转Uri

/**

* Gets the content:// URI from the given corresponding path to a file

*

* @param context

* @param imageFile

* @return content Uri

*/

public static Uri getImageContentUri(Context context, java.io.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;

}

}

}

Uri转绝对路径

/**

* Gets the corresponding path to a file from the given content:// URI

* @param selectedVideoUri The content:// URI to find the file path from

* @param contentResolver The content resolver to use to perform the query.

* @return the file path as a string

*/

public static String getFilePathFromContentUri(Uri selectedVideoUri,

ContentResolver contentResolver) {

String filePath;

String[] filePathColumn = {MediaColumns.DATA};

Cursor cursor = contentResolver.query(selectedVideoUri, filePathColumn, null, null, null);

// 也可用下面的方法拿到cursor

// Cursor cursor = this.context.managedQuery(selectedVideoUri, filePathColumn, null, null, null);

cursor.moveToFirst();

int columnIndex = cursor.getColumnIndex(filePathColumn[0]);

filePath = cursor.getString(columnIndex);

cursor.close();

return filePath;

}

绝对路径转Uri的那个方法 目前是图片文件的转换 转其他文件 只要把content后面的目录换成对应文件的归属目录就行了。。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值