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后面的目录换成对应文件的归属目录就行了。。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,933评论 18 139
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,523评论 0 17
  • The sun has his simple rode of light. The clouds are deck...
    我是呜呜阅读 434评论 0 0
  • 郑儿喜欢吃鱼,多宝鱼26块钱一斤,买了一条33块钱。 表姨来二姐家帮忙带孩子,等她来吃饭,一点多才到。 下午出去转...
    简宁思静阅读 216评论 0 0
  • 书中小结: 通信的原理和信息传播的模型 (信源)编码和最短编码 解码的规则、语法 聚类 校验位 双语对照文本、语料...
    无良笔记阅读 327评论 0 0