0

I'm using Square's Retrofit library in my app. I need to POST to a particular endpoint. There is to be a JSON payload, an Authorization header as well as images attached. My image files are currently in the Pictures Directory.

POST("/" + ENDPOINT_POST_PICTURE_SET)
@Multipart
SendPictureSetResponse sendPictureSet(@Header("Authorization") 
String token, @Part("key1") TypedString value1,
@Part("key2") TypedString value2,
@PartMap Map<Integer, TypedFile> files);

I referred to https://stackoverflow.com/a/26735421/1606296. However, I get a

Caused by: android.system.ErrnoException: open failed: ENOENT (No such   file or directory)

even though those files exist. Another issue is that I cannot see the payload in the logs(I tried adding a @body parameter : It says I cannot add this to a multipart request.) I have added files(images that I saved taken from the camera to a folder in the pictures directory), by using

Map<Integer, TypedFile> files = new HashMap<Integer, TypedFile>();
files.put(0, new TypedFile("image/jpg", 
new File(application.getImageFilePaths().get(0))));
        files.put(1, new TypedFile("image/jpg", 
new  File(application.getImageFilePaths().get(1))));
        files.put(2, new TypedFile("image/jpg", 
new File(application.getImageFilePaths().get(2))));

EDIT : Stack Trace

07-04 01:07:13.501  27594-27705/com.rohanmahale.test D/Retrofit﹕ java.io.FileNotFoundException: /storage/emulated/0/Pictures/TestApp/Current/TestApp_20150704_010712.jpg: open failed: ENOENT (No such file or directory)
        at libcore.io.IoBridge.open(IoBridge.java:456)
        at java.io.FileInputStream.<init>(FileInputStream.java:76)
        at retrofit.mime.TypedFile.writeTo(TypedFile.java:74)
        at  retrofit.mime.MultipartTypedOutput$MimePart.writeTo(MultipartTypedOutput.java:54)
at retrofit.mime.MultipartTypedOutput.writeTo(MultipartTypedOutput.java:144)
        at retrofit.Utils.readBodyToBytesIfNecessary(Utils.java:62)
        at retrofit.RestAdapter.logAndReplaceRequest(RestAdapter.java:438)
        at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:317)
        at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
        at java.lang.reflect.Proxy.invoke(Proxy.java:397)
        at $Proxy0.sendPictureSet(Unknown Source)
        at com.rohanmahale.test.helpers.WebServicesHelper.sendPicturesToServer(WebServicesHelper.java:79)
        at com.rohanmahale.test.fragments.DemoCameraFragment.sendPicsToServer(DemoCameraFragment.java:300)
        at com.rohanmahale.test.fragments.DemoCameraFragment_.access$201(DemoCameraFragment_.java:28)
        at com.rohanmahale.test.fragments.DemoCameraFragment_$6.execute(DemoCameraFragment_.java:172)
        at org.androidannotations.api.BackgroundExecutor$Task.run(BackgroundExecutor.java:393)
        at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
        at java.util.concurrent.FutureTask.run(FutureTask.java:237)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
        at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
        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:442)
at java.io.FileInputStream.<init>(FileInputStream.java:76)
at retrofit.mime.TypedFile.writeTo(TypedFile.java:74)
    at retrofit.mime.MultipartTypedOutput$MimePart.writeTo(MultipartTypedOutput.java:54)
    at retrofit.mime.MultipartTypedOutput.writeTo(MultipartTypedOutput.java:144)
            at retrofit.Utils.readBodyToBytesIfNecessary(Utils.java:62)
            at retrofit.RestAdapter.logAndReplaceRequest(RestAdapter.java:438)
            at retrofit.RestAdapter$RestHandler.invokeRequest(RestAdapter.java:317)
            at retrofit.RestAdapter$RestHandler.invoke(RestAdapter.java:240)
            at java.lang.reflect.Proxy.invoke(Proxy.java:397)
            at $Proxy0.sendPictureSet(Unknown Source)
            at com.rohanmahale.test.helpers.WebServicesHelper.sendPicturesToServer(WebServicesHelper.java:79)
            at com.rohanmahale.test.fragments.DemoCameraFragment.sendPicsToServer(DemoCameraFragment.java:300)
            at com.rohanmahale.test.fragments.DemoCameraFragment_.access$201(DemoCameraFragment_.java:28)
            at com.rohanmahale.test.fragments.DemoCameraFragment_$6.execute(DemoCameraFragment_.java:172)
            at org.androidannotations.api.BackgroundExecutor$Task.run(BackgroundExecutor.java:393)
            at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
            at java.util.concurrent.FutureTask.run(FutureTask.java:237)
            at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
            at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
            at java.lang.Thread.run(Thread.java:818)
07-04 01:07:13.501  27594-27705/com.rohanmahale.test D/Retrofit﹕ ---- END ERROR

How do I fix this?

Community
  • 1
  • 1
Rohan
  • 593
  • 7
  • 22

1 Answers1

0

I got it to work.

@POST("/" + ENDPOINT_POST_PICTURE_SET)
void sendPictureSet(@Body MultipartTypedOutput attachments,Callback<String> response)

For the MulipartTypedOutput, I used

 MultipartTypedOutput multipartTypedOutput = new MultipartTypedOutput();
 multipartTypedOutput.addPart("imageArray[]", new TypedFile("image/jpg", new File(application.getImageFilePaths().get(0))));
 multipartTypedOutput.addPart("imageArray[]", new TypedFile("image/jpg", new File(application.getImageFilePaths().get(1))));
 multipartTypedOutput.addPart("imageArray[]", new TypedFile("image/jpg", new File(application.getImageFilePaths().get(2))));
 multipartTypedOutput.addPart("key2", new TypedString("val2")); 

Thanks to @Louis Loo, https://stackoverflow.com/a/25260556/1606296

Community
  • 1
  • 1
Rohan
  • 593
  • 7
  • 22