Android 文件絕對(duì)路徑和Content開(kāi)頭的Uri互相轉(zhuǎn)換

工作中遇到的問(wèn)題。拍照獲取圖片后是得到的路徑是

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

但是我想要的路徑是:

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

這種 Uri類型的

查閱資料找到如下方法

轉(zhuǎn)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轉(zhuǎn)絕對(duì)路徑

/** 
     * 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;  
    }  

絕對(duì)路徑轉(zhuǎn)Uri的那個(gè)方法 目前是圖片文件的轉(zhuǎn)換 轉(zhuǎn)其他文件 只要把content后面的目錄換成對(duì)應(yīng)文件的歸屬目錄就行了。。

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容

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