gpt4 book ai didi

java - Android java.io.FileNotFoundException 没有这样的文件或>目录,尽管授予了权限

转载 作者:行者123 更新时间:2023-12-02 01:46:02 28 4
gpt4 key购买 nike

我正在尝试上传之前从相机拍摄的图像,并且我可以确认我已获得 READ_EXTERNAL_STORAGE 的权限。我在 Gradle 中使用 minSdkVersion 23。

图像的 Uri 保存在数据库中,当我尝试使用此 Uri 将其放入 ImageView 时。

这是我请求访问给定 Uri 的文件的位置

    if (ContextCompat.checkSelfPermission(context ,
Manifest.permission.READ_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(context as Activity,
Manifest.permission.READ_EXTERNAL_STORAGE)) {
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(context,
arrayOf(Manifest.permission.READ_EXTERNAL_STORAGE), REQUEST_PERMISSION_READ
)

}
} else {
val imageArray = post.postImages
uploadImages(imageArray!!)

}

Uri 保存在 JSONArray 中,因此我删除了不需要的字符并准备上传文件:

  val singleImage = imageUri
val substr = singleImage.replace("\"","").split("file://").last()
println("substr $substr")
var finalFile: File? = File(substr)

if(finalFile.exists()){
println("finalFile $finalFile")
}else{
println("finalFile does not exist")
}

I/System.out: substr /storage/emulated/0/customFolder/picture.jpg

I/System.out: finalFile /storage/emulated/0/customFolder/picture.jpg

这是检查文件是否存在的打印输出,看起来不存在。

java.io.FileNotFoundException:/storage/emulated/0/customFolder/picture.jpg (No such file or directory)

尽管在浏览设备上的文件时,我确实看到了图像,并且当使用相同的 Uri 将其放入 ImageView 时,图像确实显示了。

接下来的步骤是上传文件(我正在使用我的上传是由库处理的Fast Android Networking)

 AndroidNetworking.upload("http://myUrl.com")
.addMultipartFile("file", finalFile)
.setPriority(Priority.HIGH)
.build()

考虑到我的文件被认为不存在,一旦文件开始上传,我就会崩溃并显示错误消息:

FileNotFoundException: /storage/emulated/0/customFolder/picture.jpg (No such file or directory)

我收到此错误消息:

error: com.androidnetworking.error.ANError: java.lang.IllegalStateException: closed

我不确定为什么该文件未被识别和/或创建。有人能够确定为什么我的文件没有被创建吗?

最佳答案

我遇到的问题是由使用快速 Android 网络库的错误引起的。我已标记此 issue对于开发人员来说,他显然正在致力于修复。

关于java - Android java.io.FileNotFoundException 没有这样的文件或>目录,尽管授予了权限,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57454319/

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