文件File path与content:// Uri的相互转换

content Uri to path

[java]  view plain copy
  1.     /** 
  2.      * Gets the corresponding path to a file from the given content:// URI 
  3.      * @param selectedVideoUri The content:// URI to find the file path from 
  4.      * @param contentResolver The content resolver to use to perform the query. 
  5.      * @return the file path as a string 
  6.      */  
  7.     public static String getFilePathFromContentUri(Uri selectedVideoUri,  
  8.             ContentResolver contentResolver) {  
  9.         String filePath;  
  10.         String[] filePathColumn = {MediaColumns.DATA};  
  11.   
  12.         Cursor cursor = contentResolver.query(selectedVideoUri, filePathColumn, nullnullnull);  
  13. //      也可用下面的方法拿到cursor  
  14. //      Cursor cursor = this.context.managedQuery(selectedVideoUri, filePathColumn, null, null, null);  
  15.           
  16.         cursor.moveToFirst();  
  17.   
  18.         int columnIndex = cursor.getColumnIndex(filePathColumn[0]);  
  19.         filePath = cursor.getString(columnIndex);  
  20.         cursor.close();  
  21.         return filePath;  
  22.     }  

path to content Uri 

[java]  view plain copy
  1. /** 
  2.  * Gets the content:// URI  from the given corresponding path to a file 
  3.  * @param context 
  4.  * @param imageFile 
  5.  * @return content Uri 
  6.  */  
  7. public static Uri getImageContentUri(Context context, java.io.File imageFile) {  
  8.        String filePath = imageFile.getAbsolutePath();  
  9.        Cursor cursor = context.getContentResolver().query(  
  10.                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,  
  11.                new String[] { MediaStore.Images.Media._ID },  
  12.                MediaStore.Images.Media.DATA + "=? ",  
  13.                new String[] { filePath }, null);  
  14.        if (cursor != null && cursor.moveToFirst()) {  
  15.            int id = cursor.getInt(cursor  
  16.                    .getColumnIndex(MediaStore.MediaColumns._ID));  
  17.            Uri baseUri = Uri.parse("content://media/external/images/media");  
  18.            return Uri.withAppendedPath(baseUri, "" + id);  
  19.        } else {  
  20.            if (imageFile.exists()) {  
  21.                ContentValues values = new ContentValues();  
  22.                values.put(MediaStore.Images.Media.DATA, filePath);  
  23.                return context.getContentResolver().insert(  
  24.                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);  
  25.            } else {  
  26.                return null;  
  27.            }  
  28.        }  
  29.    }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值