0

我已经搜索了 open failed: ENOENT (No such file or directory) 错误的解决方案,但是已经厌倦了一切并且仍然无法正常工作。

我正在尝试将手机上的图片上传到我在服务器上创建的数据库。

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) 

那是我得到的错误信息。

这是我用来检查 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);
        }
    }
4

2 回答 2

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

如果你调用getPath()a Uri,你就做错了,除非它Uri有一个file方案。你Uri有一个content计划。不需要这样 aUri指向文件系统上您能够读取的文件。

使用ContentResolveropenInputStream()打开InputStream由 代表的内容Uri。然后,将其与任何UploadFileAsync功能一起使用。

于 2016-06-01T14:14:43.770 回答
0

确保首先创建父目录:

myFile.mkdirs();
于 2016-06-01T16:53:12.637 回答