從android中的文件路徑獲取內(nèi)容uri我知道圖像的絕對(duì)路徑(比如說/sdcard/cats.jpg)。這個(gè)文件的內(nèi)容是否有任何方法?實(shí)際上在我的代碼中我下載了一個(gè)圖像并將其保存在特定位置。為了在ImageView中設(shè)置圖像,我使用路徑打開文件,獲取字節(jié)并創(chuàng)建位圖,然后在ImageView中設(shè)置位圖。這是一個(gè)非常緩慢的過程,相反,如果我可以獲得內(nèi)容uri,那么我可以很容易地使用該方法 ImageView.setImageUri(uri)
3 回答

瀟湘沐
TA貢獻(xiàn)1816條經(jīng)驗(yàn) 獲得超6個(gè)贊
試試:
ImageView.setImageURI(Uri.fromFile(new File("/sdcard/cats.jpg")));
或者:
ImageView.setImageURI(Uri.parse(new File("/sdcard/cats.jpg").toString()));

動(dòng)漫人物
TA貢獻(xiàn)1815條經(jīng)驗(yàn) 獲得超10個(gè)贊
它來晚了,但將來可能會(huì)有所幫助。
要獲取文件的內(nèi)容URI,您可以使用以下方法:
FileProvider.getUriForFile(Context context, String authority, File file)
它返回內(nèi)容URI。

元芳怎么了
TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超7個(gè)贊
你可以嘗試下面的代碼片段
public Uri getUri(ContentResolver cr, String path){ Uri mediaUri = MediaStore.Files.getContentUri(VOLUME_NAME); Cursor ca = cr.query(mediaUri, new String[] { MediaStore.MediaColumns._ID }, MediaStore.MediaColumns.DATA + "=?", new String[] {path}, null); if (ca != null && ca.moveToFirst()) { int id = ca.getInt(ca.getColumnIndex(MediaStore.MediaColumns._ID)); ca.close(); return MediaStore.Files.getContentUri(VOLUME_NAME,id); } if(ca != null) { ca.close(); } return null;}
- 3 回答
- 0 關(guān)注
- 2883 瀏覽
添加回答
舉報(bào)
0/150
提交
取消