0

I wanna send a image to my backend but I can not and I do not know because. The backend is done with the framework Express also I tested the function and it is working but when I send from the mobile phone does not work.

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO: 12/27/2019 obtener la infomrmacion de la imagen
    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK){
        Uri photoUri = data.getData();
        user_Img.setImageURI(photoUri);
        String[] proj = { MediaStore.Images.Media.DATA };
        Cursor actualimagecursor = managedQuery(photoUri, proj,null, null, null);
        int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); actualimagecursor.moveToFirst();
        String img_path = actualimagecursor.getString(actual_image_column_index);
        Log.i("img_path", img_path);
        String [] separar = img_path.split("/");
        String nameImg = separar[6];
        Log.i("nameImg", nameImg);
        File file = new File(img_path);

        if (file.exists()){
            RequestBody requestFile = RequestBody.create(MediaType.parse("image/*"),file);
            procfile = MultipartBody.Part.createFormData("photo",file.getName(), requestFile);
        }

        router = conexion.getApiService();
        Call<ModelsMensajes> modelsMensajesCall = router.routerCrearCuenta(procfile);
        modelsMensajesCall.enqueue(new Callback<ModelsMensajes>() {
            @Override
            public void onResponse(Call<ModelsMensajes> call, Response<ModelsMensajes> response) {
                if (response.isSuccessful()){
                    String data = response.body().getMessage();
                    Toast.makeText(RegisterActivity.this, "" + data, Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public void onFailure(Call<ModelsMensajes> call, Throwable t) {

            }
        });

    }
}

 @Multipart
@POST("user/test")
Call<ModelsMensajes> routerCrearCuenta(@Part MultipartBody.Part photo);

This is the function that have the backend. The route and the json that back.

router.post('/test', multipartMiddleware, test);


export async function test(req,res){
const urlPhoto = req.files.photo;

res.json({
message: urlPhoto
})

}

{
  "message": {
  "fieldName": "photo",
  "originalFilename": "icons8-docker-48.png",
  "path": "src/photos/B90paORGrFnWsr4jl-vVvUJa.png",
   "headers": {
     "content-disposition": "form-data; name=\"photo\"; filename=\"icons8-docker-48.png\"",
     "content-type": "image/png"
   },
   "size": 1190,
   "name": "icons8-docker-48.png",
   "type": "image/png"
  }
}
Bruno
  • 3,872
  • 4
  • 20
  • 37
  • _"when I send from the mobile phone does not work"_ the thing is you should not ignore the error inside `onFailure()` method. Here you get an idea about the error or what's going wrong while sending a request to API. Log the error like `Log.e("ERROR", t);` and [edit] the question, post the error logs here. And also you said that you tested the API and it works right? How did you test it? Can you share the screenshots here (if you've tested it with RestAPI testing tool like Postman)? – Shashanth Jan 09 '20 at 05:59
  • Hello, user retrofit library which will make it super easy for you. Refer: https://stackoverflow.com/a/39953566/10941112 – Saswata Jan 09 '20 at 06:19
  • Thanks @Shashanth. The error is E/Error: java.io.FileNotFoundException: /storage/emulated/0/DCIM/Camera/IMG_20200107_220206.jpg: open failed: EACCES (Permission denied) –  Jan 09 '20 at 13:04
  • Does this answer your question? [Exception 'open failed: EACCES (Permission denied)' on Android](https://stackoverflow.com/questions/8854359/exception-open-failed-eacces-permission-denied-on-android) – Shashanth Jan 09 '20 at 13:18
  • Yeah!! @Shashanth but I have other error :( E/Error: com.google.gson.JsonSyntaxException: java.lang.IllegalStateException: Expected a string but was BEGIN_OBJECT at line 1 column 13 path $.message –  Jan 09 '20 at 13:36
  • Comments are meant to ask clarifications or suggest improvements. Don't post error logs or codes in comments. Instead [edit] your question and post it. – Shashanth Jan 09 '20 at 13:41

0 Answers0