Android java.io.FileNotFoundException No such file or > directory, although permissions are granted

1.8k Views Asked by At

I am trying to upload an image that was previously taken from the camera, and I can confirm that I am granted the permission to READ_EXTERNAL_STORAGE. I am using minSdkVersion 23 in Gradle.

Th Uri of the image is saved in a database, and when I try to use this Uri to put it into an ImageView.

Here is where I request to access the file at the given 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!!)

    }

The Uri is kept within a JSONArray, so I remove the unwanted characters and prepare the file for upload:

  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

Here is the print out to check if the files does exist, and it looks like it doesn't.

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

Although when going through the files on my device I do see the image, and when using that same Uri to put it into an imageview, the image does show up.

The following step is uploading the file (I am using my upload is handled by the Library Fast Android Networking)

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

Considering my File is considered non existent, I get a crash with the error message once the File begins uploading:

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

and I get this error message:

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

I'm not sure why the File isn't being recognized and/or created. Is anyone able to identify why my file isn't getting created?

1

There are 1 best solutions below

0
On BEST ANSWER

The problem I am experiencing was caused by a bug using the Fast Android Networking library. I have flagged this issue to the developer, he is apparently working on a fix.