亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將content:// URI轉換為Android 4.4中的實際路徑

將content:// URI轉換為Android 4.4中的實際路徑

一只萌萌小番薯 2019-12-10 13:09:14
我嘗試了一種運行良好的解決方案(請參見下文),但在Android 4.4中,該調用調出startActivityForResult()了一個名為“打開自”的活動,該活動包含“最近”,“圖像”,“下載”以及可供選擇的多個應用。當我選擇“圖像”并嘗試解析返回的內容URI(使用下面的代碼)時,調用cursor.getString()返回null。如果我使用Gallery應用程序選擇了完全相同的文件,則cursor.getString()返回文件路徑。我只在API級別16和19中對此進行了測試。一切都可以在16中按預期進行。就19而言,我必須選擇Gallery或其他應用程序,否則它將不起作用。private String getRealPathFromURI(Context context, Uri contentUri) {    Cursor cursor = null;    try {         String[] proj = { MediaStore.Images.Media.DATA };        cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);        cursor.moveToFirst();        String path = cursor.getString(column_index);        return path;    } finally {        if (cursor != null) {            cursor.close();        }    }}
查看完整描述

3 回答

?
翻翻過去那場雪

TA貢獻2065條經驗 獲得超14個贊

將content:// URI轉換為Android 4.4中的實際路徑


在任何Android版本上都沒有可靠的方法來執行此操作。A content:// Uri不必代表文件系統上的文件,更不用說您可以訪問的文件了。


Android 4.4提供存儲框架的更改只是增加了您遇到content:// Uri值的頻率。


如果得到a content:// Uri,請使用ContentResolver和方法(例如openInputStream()和)使用它openOutputStream()。



查看完整回答
反對 回復 2019-12-12
?
森林海

TA貢獻2011條經驗 獲得超2個贊

我也一直面臨這個問題,但就我而言,我想做的就是為Gallery指定一個具體的Uri,以便以后可以使用裁剪。看起來在新的KitKat文檔瀏覽器中,除非您在導航抽屜中選擇galery,然后像您說的那樣直接從那里打開圖像或文件,否則我們將無法再這樣做。


在Uri情況下,從文檔瀏覽器打開時,您仍然可以檢索路徑。


    Intent dataIntent= new Intent(Intent.ACTION_GET_CONTENT);

    dataIntent.setType("image/*"); //Or whatever type you need

然后在onActivityResult中:


@Override

public void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == ACTIVITY_SELECT_IMAGE && resultCode == RESULT_OK) {

        myUri = data.getData();

        String path = myUri.getPath();

        openPath(myUri);


    }

}

如果您需要然后使用該路徑打開文件,則只需使用Content Resolver:


public void openPath(Uri uri){

    InputStream is = null;

    try {

        is = getContentResolver().openInputStream(uri);

        //Convert your stream to data here

        is.close();


    } catch (FileNotFoundException e) {

        e.printStackTrace();

    } catch (IOException e) {

        // TODO Auto-generated catch block

        e.printStackTrace();

    }

}



查看完整回答
反對 回復 2019-12-12
  • 3 回答
  • 0 關注
  • 529 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號