0

I am getting these errors when I am uploading images. The path is storing in database table, But the image folder does not have image.

These are the errors

E/ErrorUtil: Error: /document/primary:DCIM/Camera/20190804_111501.jpg (No such file or directory) W/System.err: java.io.FileNotFoundException: /document/primary:DCIM/Camera/20190804_111501.jpg (No such file or directory)

Here I shared my code.

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
    super.onActivityResult(requestCode, resultCode, data)

    if (requestCode == PICK_IMAGE_MULTIPLE && resultCode == Activity.RESULT_OK
            && null != data) {

        if(data.getClipData() != null) {
            val count = data.getClipData().getItemCount(); //evaluate the count before the for loop --- otherwise, the count is evaluated every loop.
            for(i in 0..count-1) {
                val imageUri : Uri = data.getClipData().getItemAt(i).uri
               // getRealPathFromURI(applicationContext,imageUri)

                imageUri.let {
                    Log.d("imageUri",it.toString())
                    Log.d("imagePath",it.path)
                    file = File(it.path)
                    val jj=Gson()
                    Log.d("fileIS",jj.toJson(file))
                    fileType = EnumUtils.FileType.IMAGE.value
                    val name = file!!.name
                    Log.d("fimenameis",name)
                    multipartArray!!.add(MultipartBody.Part.createFormData("files[]", file!!.name, RequestBody.create(MediaType.parse(file!!.path.getMimeType()?: ""), file!!)))
                    val s = Gson()
                    Log.d("multipartArray",s.toJson(multipartArray).toString())
                }

            }

            //do something with the image (save it to some directory or whatever you need to do with it here)
    } else if(data.getData() != null) {
        val imagePath = data.getData().getPath();
            fileType = EnumUtils.FileType.IMAGE.value
            file = File(imagePath)
            multipartArray!!.add(MultipartBody.Part.createFormData("files[]", file!!.name, RequestBody.create(MediaType.parse(file!!.path.getMimeType()?: ""), file!!)))
            Log.d("imagePath",imagePath.toString())
        //do something with the image (save it to some directory or whatever you need to do with it here)
    }

    }
Benoit
  • 5,118
  • 2
  • 24
  • 43
jithin mp
  • 95
  • 2
  • 10

1 Answers1

0

yes it happen some time due to file path does not get the absolute path and failed to convert it into file

      ` private fun getRealPathFromURI(contentURI: Uri): String? {
    val result: String?
    val cursor = contentResolver.query(contentURI, null, null, null, null)
    if (cursor == null) { // Source is Dropbox or other similar local file path
        result = contentURI.path
    } else {
        cursor.moveToFirst()
        val idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA)
        result = cursor.getString(idx)
        cursor.close()
    }
    return result
}`

 val file: File = File(getRealPathFromURI(Uri.parse(imagePath)))
Amit pandey
  • 1,149
  • 1
  • 4
  • 15
  • i got the error -> Make sure the Cursor is initialized correctly before accessing data from it. – jithin mp Mar 05 '20 at 04:44
  • try this or you can simple add your error in google and find many solution like this https://stackoverflow.com/questions/20240249/make-sure-the-cursor-is-initialized-correctly-before-accessing-data-from-it – Amit pandey Mar 05 '20 at 05:32