Reputation: 101
Hello I am working with android . I want to save multiple captured images in a folder(programmatically created in internal storage of the device) and read this file from the folder and send to server as multipart form. I used the below code for attaching images to 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());
}
}
When I use only one image in this loop it works fine. When more than one images, it cause the following exception
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).
I added permissions in the manifest
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
my complete multpart uploading method is
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();
I had searched for this file in my folder manually and it exist in the same folder.Then why I got this file not found exception ? How can I resolve this issue? please help.
Upvotes: 0
Views: 2760
Reputation: 3532
You have an extra white space in your path.
Try with this
file = new File(android.os.Environment.getExternalStorageDirectory() + "/MyFolder/" + imagePath.get(0).toString().trim());
Upvotes: 4