[英]Android - load photo from uri
我正在嘗試通過 Uri 將圖像上傳到 imageview,但它不起作用。
java.io.FileNotFoundException: /file:/data/user/0/com.maksu.aquarium/cache/cropped6681458112782885202.jpg (No such file or directory)
這是我上傳照片的方式:
Uri profileUri = Uri.fromFile(new File("file:///data/user/0/com.maksu.aquarium/cache/cropped6681458112782885202.jpg"));
Glide.with(this).load(profileUri).into(akvaryumDialogResim);
首先,永遠不要對路徑進行硬編碼。 出於此答案的目的,我將堅持您的硬編碼值,但您確實需要一種更好的方式來處理您的文件。 例如,您的硬編碼路徑對於很多用戶來說都是錯誤的。
其次, file:///data/user/0/com.maksu.aquarium/cache/cropped6681458112782885202.jpg
不是文件系統路徑。 這是一個Uri
。 Android 基於 Linux,其文件系統路徑以/
開頭。 file://
是一個Uri
方案,就像https://
是這個網頁的 URL 中使用的Uri
方案一樣。
所以,要么使用:
Uri profileUri = Uri.parse("file:///data/user/0/com.maksu.aquarium/cache/cropped6681458112782885202.jpg");
或使用:
Uri profileUri = Uri.fromFile(new File("/data/user/0/com.maksu.aquarium/cache/cropped6681458112782885202.jpg"));
要訪問緩存文件夾中的文件,您可以使用Context.getCacheDir()
方法而不是硬編碼完整路徑。 所以,試試:
Uri profileUri = Uri.fromFile(new File(geCacheDir(), "cropped6681458112782885202.jpg"));
Glide.with(this).load(profileUri).into(akvaryumDialogResim);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.