java - Android - 从 uri 加载照片

标签 java android mobile android-glide

我正在尝试通过 Uri 将图像上传到 imageview,但它不起作用。

java.io.FileNotFoundException: /file:/data/user/0/com.maksu.aquarium/cache/cropped6681458112782885202.jpg (No such file or directory)

我收到上述错误。当我检查时,我看到图片存在。 My photo path

这就是我上传照片的方式:

     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/

相关文章:

java - 通用 Java 移动开发

java - 我的 GMF 生成的图表代码有什么问题?

java - 异常java.lang.IllegalStateException: Activity 已被破坏

android - 当我从“运行”菜单中选择“停止”时,为什么模拟器窗口不会在 Android Studio 1.1.0 中关闭?

android-layout - Dart 中是否有 fastclick 库(或其他解决方案)来消除移动设备上的 300ms 延迟(ghostclick)?

android - 如何在 Android 中正确加载存档网页?

Java 8 : Difference between two LocalDateTime in multiple units

java - 调用 CallableStatement.execute() 时出现问题;

android - 如何修复 “ERROR: Unable to resolve dependency for ':app @ debug/compileClasspath':。” Android错误

android - 想在 Button 上添加图标,drawableTop 在 Android 上不起作用