打开失败:使用getAbsolutePath()ENOENT

时间:2016-06-01 14:05:46

标签: java android enoent

我已经搜索了打开失败的解决方案:ENOENT(没有这样的文件或目录)错误但是已经厌倦了所有内容并且仍然无法正常工作。

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

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);
        }
    }

2 个答案:

答案 0 :(得分:0)

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

如果您在getPath()上致电Uri,那么您做错了,除非Urifile计划。您的Uri有一个content计划。不需要这样的Uri指向您能够读取的文件系统上的文件。

使用ContentResolveropenInputStream()InputStream代表的内容上打开Uri。然后,将其用于UploadFileAsync所做的任何事情。

答案 1 :(得分:0)

确保首先创建父目录:

myFile.mkdirs();