I've seen variations on this issue and tried a whole bunch of different things, including encoding the whole thing as a string and so on. Basically, I am getting an array of image path(s) from and Android image picker library (Fishbun), then successfully passing that array to another fragment, which is the fragment that is supposed to upload it.
From there I'm using Koush's Ion framework to do all the networking (99.99% of the networking in the app is using this so it's preferable, although I'm open to other suggestions if this turns out to be the issue).
Relevant methods within uploading fragment:
private void uploadImage(String adId, String fileUri, Context context){
final ProgressDialog progress=new ProgressDialog(context);
progress.setMessage("Uploading Images");
progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progress.setIndeterminate(false);
progress.setProgress(0);
progress.show();
String url = <URL HERE>;
Log.d(TAG,"Uri: "+ fileUri);
Uri uri = Uri.parse(fileUri);
// creating a file body consisting of the file that we want to
// send to the server
File bin = new File(getRealPathFromURI(uri));
if(bin != null){
Ion.with(getContext())
.load("POST",url)
.uploadProgressDialog(progress).setMultipartFile("image", bin).asJsonObject().setCallback(new FutureCallback<JsonObject>() {
@Override
public void onCompleted(Exception e, JsonObject result) {
if(e != null){
Log.e("error", e.getLocalizedMessage());
}else{
Log.e("result", result.getAsString());
}
}
});
}else {
Log.d(TAG, "couldn't create file");
}
}
private String getRealPathFromURI(Uri contentURI) {
String result;
Cursor cursor = getContext().getContentResolver().query(contentURI, null, null, null, null);
if (cursor == null) { // Source is Dropbox or other similar local file path
result = contentURI.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
result = cursor.getString(idx);
cursor.close();
}
return result;
}
Relevant Stack Trace:
E/error: /api/v2/adverts/102847259/images: open failed: ENOENT (No such file or directory)
This is what the file path is looking like when it gets to the fragment, which I then turn into the real file path using the method above:
/storage/emulated/0/Pictures/Screenshots/Screenshot_20160516-130248.png
Looks like your URL is a file path and not an http url. ENOENT indicates it is trying to open a local file, and failing. Check the value in a debugger.