gpt4 book ai didi

java - 当我使用 FileInputStream 时出现 FileNotFoundException

转载 作者:行者123 更新时间:2023-11-29 20:57:08 25 4
gpt4 key购买 nike

在我的 Android 应用程序中,用户必须在手机上选择一张他想上传到服务器的图片。我检索了路径,但是当我初始化 FileInputStream 对象时,出现错误。

这是我的代码:

File file = new File(imageURI.getPath());
FileInputStream fileInputStream = new FileInputStream(file);

我遇到了这个错误:

java.io.FileNotFoundException: /external/images/media/30732: open failed: ENOENT (No such file or directory)

但是文件存在,因为用户可以选择它。

在 list 中,有 WRITE_EXTERNAL_STORAGE 权限。所以问题不在于此。

问题出在哪里?

最佳答案

你有一个 content Uri , 登录以查看它。getPath 返回与文件路径无关的 Uri 的路径部分。

尝试使用:

InputStream imageStream = getContext().getContentResolver().openInputStream(imageURI);

关于java - 当我使用 FileInputStream 时出现 FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27300916/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com