Android java.io.FileNotFoundException图像打开失败:来自内部存储的ENOENT

时间:2016-01-22 07:02:54

标签: java android image file multipart

您好我正在使用android。我想将多个捕获的图像保存在一个文件夹中(以编程方式在设备的内部存储中创建),并从该文件夹中读取该文件并以多部分形式发送到服务器。我使用下面的代码将图像附加到multipart,

for(int i=0;i<Captured_imagePath.size();i++) {
   try {
      final File file2 = new File(android.os.Environment.getExternalStorageDirectory() + "/MyFolder/" + Captured_imagePath.get(i));

      final FileBody fileBody = new FileBody(file2);
      entityBuilder.addPart(file2.getName(), fileBody);
      entityBuilder.setBoundary(boundary);
   } catch (Exception e) {
      Log.i(TAG, "Except:: " + e.toString());
   }
}

当我在这个循环中只使用一个图像时,它工作正常。当有多个图像时,会导致以下异常

 8594-9144/? W/System.err﹕ java.io.FileNotFoundException: /storage/sdcard0/MyFolder/ 20160123_121858.jpg: open failed: ENOENT (No such file or directory)
01-23 12:19:09.525    8594-9144/? W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
01-23 12:19:09.525    8594-9144/? W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
01-23 12:19:09.525    8594-9144/? W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
01-23 12:19:09.525    8594-9144/? W/System.err﹕ Caused by: libcore.io.ErrnoException: open failed: ENOENT (No such file or directory).

我在清单中添加了权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

我的完整多部分上传方法是

   public String uploadMultipart(String url,String json ,ArrayList<String> imagePath,ArrayList<String> Captured_imagePath) {
        String result="";
        File  file=null;
        String boundary = "-------------" + System.currentTimeMillis();



        try
        {
            HttpClient client = new DefaultHttpClient();
            HttpPost post = new HttpPost(url);

            MultipartEntityBuilder entityBuilder = MultipartEntityBuilder.create();
            entityBuilder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);

            entityBuilder.addTextBody("json", json);
            entityBuilder.setBoundary(boundary);
            if(imagePath.size()!=0)
            {
                file = new File(android.os.Environment.getExternalStorageDirectory() + "/MyFolder/" + imagePath.get(0));

                entityBuilder.addBinaryBody(file.getName(), file,  ContentType.create("image/jpeg"), imagePath.get(0));

                entityBuilder.setBoundary(boundary);
            }

            if(Captured_imagePath.size()!=0)
            {

                for(int i=0;i<Captured_imagePath.size();i++)
                {

                    try {


                        final File file2 = new File(android.os.Environment.getExternalStorageDirectory() + "/MyFolder/" + Captured_imagePath.get(i));



                        final FileBody fileBody = new FileBody(file2);
                        entityBuilder.addPart(file2.getName(), fileBody);
                        entityBuilder.setBoundary(boundary);

                    }catch (Exception e)
                    {
                        Log.i(TAG, "Except:: " + e.toString());
                    }

                }


            }


            HttpEntity entity = entityBuilder.build();
            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
            entity.writeTo(bytes);
            String content = bytes.toString();

        //    Log.i(TAG,"sending data"+content);
          //  writeToFile("shanu",content);

            post.setEntity(entity);
            HttpResponse response = client.execute(post);
            HttpEntity httpEntity = response.getEntity();
            result = EntityUtils.toString(httpEntity);
            Log.i(TAG2, result);
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        return result.toString();

我手动搜索了我的文件夹中的这个文件,它存在于同一个文件夹中。那么为什么我找不到这个文件的例外?我该如何解决这个问题?请帮忙。

1 个答案:

答案 0 :(得分:4)

您的路径中有一个额外的空白区域。

试试这个

file = new File(android.os.Environment.getExternalStorageDirectory() + "/MyFolder/" + imagePath.get(0).toString().trim());