【发布时间】:2019-08-11 23:23:52
【问题描述】:
我正在尝试上传以前从相机拍摄的图像,我可以确认我已获得READ_EXTERNAL_STORAGE 的权限。我在 Gradle 中使用 minSdkVersion 23。
图像的Th 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(没有这样的文件或目录)
虽然在我的设备上浏览文件时我确实看到了图像,但当使用相同的 Uri 将其放入图像视图时,图像确实会显示出来。
接下来的步骤是上传文件(我正在使用我的上传由库Fast Android Networking处理)
AndroidNetworking.upload("http://myUrl.com")
.addMultipartFile("file", finalFile)
.setPriority(Priority.HIGH)
.build()
考虑到我的文件被认为不存在,一旦文件开始上传,我就会崩溃并显示错误消息:
FileNotFoundException: /storage/emulated/0/customFolder/picture.jpg(没有这样的文件或 目录)
我收到此错误消息:
错误:com.androidnetworking.error.ANError: java.lang.IllegalStateException: 关闭
我不确定为什么无法识别和/或创建文件。有没有人能够确定为什么我的文件没有被创建?
【问题讨论】:
-
您实际上并未在问题中说明问题。在标题中,您说您遇到了
FileNotFoundException,但到目前为止,您只发布了有效的代码部分。您需要添加实际导致问题的代码,并指出引发异常的位置。 -
感谢@LeoAso 告诉我,我已经添加了有关我的问题的更多详细信息。
标签: java android kotlin android-permissions fast-android-networking