我正在尝试通过 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://
是一个 Uri
方案此网页的 URL。
所以,要么使用:
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"));
关于java - Android - 从 uri 加载照片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59118963/