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后面的目錄換成對應文件的歸屬目錄就行了。。

最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關閱讀更多精彩內(nèi)容

  • Spring Cloud為開發(fā)人員提供了快速構建分布式系統(tǒng)中一些常見模式的工具(例如配置管理,服務發(fā)現(xiàn),斷路器,智...
    卡卡羅2017閱讀 136,537評論 19 139
  • ¥開啟¥ 【iAPP實現(xiàn)進入界面執(zhí)行逐一顯】 〖2017-08-25 15:22:14〗 《//首先開一個線程,因...
    小菜c閱讀 7,319評論 0 17
  • The sun has his simple rode of light. The clouds are deck...
    我是嗚嗚閱讀 471評論 0 0
  • 鄭兒喜歡吃魚,多寶魚26塊錢一斤,買了一條33塊錢。 表姨來二姐家?guī)兔Ш⒆?,等她來吃飯,一點多才到。 下午出去轉...
    簡寧思靜閱讀 239評論 0 0
  • 書中小結: 通信的原理和信息傳播的模型 (信源)編碼和最短編碼 解碼的規(guī)則、語法 聚類 校驗位 雙語對照文本、語料...
    無良筆記閱讀 371評論 0 0

友情鏈接更多精彩內(nèi)容