I'm picking an image from my android emulator gallery in order to serve it to the server but whenever I tried that, I got this error D/Main: /document/1AE6-2C19:Download/food2.jpg: open failed: ENOENT (No such file or directory). I have tried all the so开发者_StackOverflow社区lutions for similar problems from stack overflow here yet that couldn't be resolved. Here is the code
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode== PICK_IMAGE_REQUEST && resultCode== RESULT_OK ){
profileImageUri = data.getData();
try {
Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),profileImageUri);
profilePicture.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
profileImagePath = profileImageUri.getPath();
}
}
public void uploadProfileImage(){
File uploadFile = new File(profileImagePath);
Log.d("Main","The uploaded file name is "+uploadFile.getName());
RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),uploadFile);
MultipartBody.Part body = MultipartBody.Part.createFormData("file",uploadFile.getName(),requestBody);
Call<ImageDto> imageDtoCall = BaseUrlClient
.getInstance().getApi().uploadProfileImage(body);
imageDtoCall.enqueue(new Callback<ImageDto>() {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onResponse(Call<ImageDto> call, Response<ImageDto> response) {
if (response.code()==201){
assert response.body() != null;
Log.d("Main","The image url is "+response.body().getDownloadUrl());
}else {
Log.d("Main","Response code is "+response.code());
Toast.makeText(PatientRegistrationActivity.this, "Error Uploading Profile Image, Please Try Again", Toast.LENGTH_SHORT).show();
try {
Log.d("Main",response.errorBody().string());
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Override
public void onFailure(Call<ImageDto> call, Throwable t) {
Log.d("Main",t.getMessage());
Toast.makeText(PatientRegistrationActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
精彩评论