N
N
Nubbin2017-08-07 05:52:37
Android
Nubbin, 2017-08-07 05:52:37

rerofit2 upload image?

Good morning, guys, I've been racking my brains for an hour now and I can't figure out what the problem is, the photo on the server doesn't want to upload in any way.

public interface UploadImageInterface {
    @Multipart
    @POST("/imagefolder/index.php")
    Call<UploadObject> uploadFile(@Part MultipartBody.Part file, @Part("name") RequestBody name);
}

public class UploadObject {
    private String success;
    public UploadObject(String success) {
        this.success = success;
    }
    public String getSuccess() {
        return success;
    }
}

File file = new File(filePath);
RequestBody mFile = RequestBody.create(MediaType.parse("image/*"), file);
                MultipartBody.Part fileToUpload = MultipartBody.Part.createFormData("file", file.getName(), mFile);
                RequestBody filename = RequestBody.create(MediaType.parse("text/plain"), file.getName());
                
Call<UploadObject> fileUpload = uploadImage.uploadFile(fileToUpload, filename);
                fileUpload.enqueue(new Callback<UploadObject>() {
                    @Override
                    public void onResponse(Call<UploadObject> call, Response<UploadObject> response) {
                        Toast.makeText(MainActivity.this, "Response " + response.raw().message(), Toast.LENGTH_LONG).show();
                        Toast.makeText(MainActivity.this, "Success " + response.body().getSuccess(), Toast.LENGTH_LONG).show();
                    }
                    @Override
                    public void onFailure(Call<UploadObject> call, Throwable t) {
                        Log.d(TAG, "Error " + t.getMessage());
                    }
                });

08-07 02:46:44.602 16819-16819/com.test.my W/System.err: java.io.FileNotFoundException: /storage/emulated/0/IMG_20170806_1627451211839253.jpg
08-07 02:46:44.603 16819-16819/com.test.my W/System.err:  (No such file or directory)
08-07 02:46:44.603 16819-16819/com.test.my W/System.err:     at java.io.FileInputStream.open(Native Method)

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
aol-nnov, 2017-08-07
@aol-nnov

all of a sudden, you need to check if your application has permissions :)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question