0

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
Bec Martin
  • 23
  • 1
  • 1
  • 7

1 Answers1

1

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.

koush
  • 2,972
  • 28
  • 31
  • 1
    The man, the myth, the legend! Thanks for replying to my post, you were absolutely correct my url was missing the front half and I though I was passing it a full URL and I wasn't. Once I eliminated that, it was much easier to nail the rest of the construction which ended up being a bit more complicated than the post above due to specifics of the API I'm using and the images in question. Thanks for the awesome library. Will be using Ion in all my projects after having been introduced to it by a colleague. – Bec Martin Jun 05 '16 at 07:58
  • 1
    FWIW, you should pass the fragment value to Ion.with(fragment), so if the fragment gets destroyed or removed, the request gets cancelled. By passing in the context, the request will stay alive until the context (I'm guessing the calling activity) dies. – koush Jun 05 '16 at 19:22