0

I have search up for solutions for open failed: ENOENT (No such file or directory) error but have tired everything and still doesnt work.

I am trying to upload the pictures on my phone to a database that i created on a server.

06-01 15:59:32.631 25399-26074/com.example.vikhram.projectv E/Debug: error: /document/primary:Pictures/Pic0601_14Lat_65.6172057.jpeg: open failed: ENOENT (No such file or directory)
                                                                 java.io.FileNotFoundException: /document/primary:Pictures/Pic0601_14Lat_65.6172057.jpeg: open failed: ENOENT (No such file or directory)
                                                                     at libcore.io.IoBridge.open(IoBridge.java:452)
                                                                     at java.io.FileInputStream.<init>(FileInputStream.java:76)
                                                                     at com.example.vikhram.projectv.MainActivity$UploadFileAsync.doInBackground(MainActivity.java:406)
                                                                     at com.example.vikhram.projectv.MainActivity$UploadFileAsync.doInBackground(MainActivity.java:375)
                                                                     at android.os.AsyncTask$2.call(AsyncTask.java:295)
                                                                     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
                                                                     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                                     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                     at java.lang.Thread.run(Thread.java:818)
                                                                  Caused by: android.system.ErrnoException: open failed: ENOENT (No such file or directory)
                                                                     at libcore.io.Posix.open(Native Method)
                                                                     at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
                                                                     at libcore.io.IoBridge.open(IoBridge.java:438)
                                                                     at java.io.FileInputStream.<init>(FileInputStream.java:76) 
                                                                     at com.example.vikhram.projectv.MainActivity$UploadFileAsync.doInBackground(MainActivity.java:406) 
                                                                     at com.example.vikhram.projectv.MainActivity$UploadFileAsync.doInBackground(MainActivity.java:375) 
                                                                     at android.os.AsyncTask$2.call(AsyncTask.java:295) 
                                                                     at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
                                                                     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
                                                                     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                                                                     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                                                                     at java.lang.Thread.run(Thread.java:818) 
06-01 15:59:32.631 25399-26074/com.example.vikhram.projectv E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
                                                                          Process: com.example.vikhram.projectv, PID: 25399
                                                                          java.lang.RuntimeException: An error occurred while executing doInBackground()
                                                                              at android.os.AsyncTask$3.done(AsyncTask.java:309)
                                                                              at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)
                                                                              at java.util.concurrent.FutureTask.setException(FutureTask.java:223)
                                                                              at java.util.concurrent.FutureTask.run(FutureTask.java:242)
                                                                              at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
                                                                              at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
                                                                              at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
                                                                              at java.lang.Thread.run(Thread.java:818)
                                                                           Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.io.InputStream java.net.HttpURLConnection.getInputStream()' on a null object reference
                                                                              at com.example.vikhram.projectv.MainActivity$UploadFileAsync.doInBackground(MainActivity.java:459)
                                                                              at com.example.vikhram.projectv.MainActivity$UploadFileAsync.doInBackground(MainActivity.java:375)
                                                                              at android.os.AsyncTask$2.call(AsyncTask.java:295)
                                                                              at java.util.concurrent.FutureTask.run(FutureTask.java:237)
                                                                              at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) 
                                                                              at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) 
                                                                              at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) 
                                                                              at java.lang.Thread.run(Thread.java:818) 

That is the error message that I get.

This is the code I run to check the getAbsolutePath();

if (requestCode == READ_REQUEST_CODE && resultCode == RESULT_OK) {
        // The document selected by the user won't be returned in the intent.
        // Instead, a URI to that document will be contained in the return intent
        // provided to this method as a parameter.
        // Pull that URI using resultData.getData().
        //Uri uri = null;
        if (data != null) {
            //super.onActivityResult(requestCode, resultCode, data);
            Uri selectedImageURI = data.getData();
           File myFile = new File(selectedImageURI.getPath());
            //File myFile = new File(data.getData().toString());
            //File imageFile = new File(getRealPathFromURI(selectedImageURI));
            //new UploadFileAsync().execute(myFile.getAbsolutePath().toString());
            new UploadFileAsync().execute(myFile.getAbsolutePath().toString());
            //Log.i(TAG, "Uri: " + selectedImageURI.toString());
            //showImage(uri);
        }
    }

2 Answers2

0
File myFile = new File(selectedImageURI.getPath());

If you call getPath() on a Uri, you are doing it wrong, unless that Uri has a file scheme. Your Uri has a content scheme. There is no requirement for such a Uri to point to a file on the filesystem that you are capable of reading from.

Use ContentResolver and openInputStream() to open an InputStream on the content represented by the Uri. Then, use that with whatever UploadFileAsync does.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
0

Make sure you create the parent directories first:

myFile.mkdirs();
xdevs23
  • 3,824
  • 3
  • 20
  • 33