[問題] 從content:// URI資源反查檔案位置
我要讓使用者選擇檔案,方法是用Intent + ACTION_GET_CONTENT (chooser)
選擇檔案後回傳的資源可分為 content:/和 file:// URI
如果是file:// 格式,就把路徑抓出來即可,因為其實就是指向sdcard下的檔案
問題出在反查content:/ URI的時候,會得到奇怪的路徑:
1. 得到content URI:content://media/xxxx/<資源編號>
2. 反查該資源對應的路徑:/storage/sdcard0/Pictures/<檔案名稱>
3. 用File物件開啟檔案 ... 就掛了
請問 storage/sdcard0/Pictures/ 其實是裝置名稱而不是掛載目錄名稱嗎,
有別的方法可以拿到目錄名稱嗎,或是這樣的結果是正確的?
---------------------------
content URI 反查路徑程式片段
ContentResolver res = getContentResolver();
Cursor c=res.query(uri, new String[]{"_data"}, null, null,null);
int index = c.getColumnIndexOrThrow("_data");
c.moveToFirst();
path=c.getString(index);
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 118.161.32.68
→
02/24 02:37, , 1F
02/24 02:37, 1F
→
02/24 02:37, , 2F
02/24 02:37, 2F
→
02/24 02:37, , 3F
02/24 02:37, 3F
→
02/24 02:38, , 4F
02/24 02:38, 4F
→
02/24 02:39, , 5F
02/24 02:39, 5F
→
02/24 02:40, , 6F
02/24 02:40, 6F
→
02/24 02:41, , 7F
02/24 02:41, 7F
應該說檔案實際位置是 /sdcard/Pictures/<檔案>
但是用URI查詢出來是 /storage/sdcard0/Pictures/<檔案>
導致File object找不到檔案
→
02/24 10:58, , 8F
02/24 10:58, 8F
有可能,雖然反查路徑成功,但是無法讀取...
不過這個路徑在DDMS看不到耶
雖然暫時可以自己硬改為正確的路徑啦
不過這可能不是正規做法
※ 編輯: codemonkey 來自: 118.161.32.68 (02/24 13:35)
→
02/24 14:57, , 9F
02/24 14:57, 9F
→
02/24 14:58, , 10F
02/24 14:58, 10F
→
02/24 14:58, , 11F
02/24 14:58, 11F
→
02/24 14:59, , 12F
02/24 14:59, 12F
→
02/24 14:59, , 13F
02/24 14:59, 13F
→
02/24 15:01, , 14F
02/24 15:01, 14F
→
02/24 15:02, , 15F
02/24 15:02, 15F
→
02/24 15:03, , 16F
02/24 15:03, 16F
→
02/24 15:03, , 17F
02/24 15:03, 17F