我想使用改造将图像发送到服务器,但是我遇到了服务器无法处理图像的问题。同时,当我测试使用邮递员发送图像时,它可以工作。我需要将文件转换为特定格式吗?
的样本请求ps:我从这里获得了FormUtils类 https://github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java
下面是我的代码
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.personal_data_step, container, false);
ButterKnife.bind(this, view);
//imageEktp.setClickable(true);
getPhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//Log.d("test : ", "testing");
CharSequence menu[] = new CharSequence[]{"Take From Galery", "Open Camera"};
AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
builder.setTitle("Pick a Picture");
builder.setItems(menu, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
if(i == 0){
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(intent, SELECT_FILE);
}else if(i == 1){
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST_CODE);
}
}
});
builder.show();
}
});
return view;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Context applicationContext = getActivity();
if(resultCode== Activity.RESULT_OK) {
if (requestCode == CAMERA_REQUEST_CODE) {
Bundle bundle = data.getExtras();
final Bitmap bitmap = (Bitmap) bundle.get("data");
imageEktp.setImageBitmap(bitmap);
/*Uri tempUri = getImageUri(getActivity().getApplicationContext(), bitmap);
File file = new File(get);
RequestBody requestFile = RequestBody.create(MediaType.parse(getContentResolver().getType()))
try {
InputStream streamCamera = applicationContext.getContentResolver().openInputStream(bitmap);
uploadImage(getBytes(streamCamera));
} catch (IOException e) {
e.printStackTrace();
}*/
} else if (requestCode == SELECT_FILE) {
Uri selectedImage = data.getData();
imageEktp.setImageURI(selectedImage);
uploadFile(selectedImage);
Log.d("selected image : ", selectedImage.toString());
}else{
Toast.makeText(getActivity(), "You haven't picked Image",Toast.LENGTH_LONG).show();
}
}
}
private void uploadFile(Uri fileUri){
ApiInterface apiService = ApiClient.getClient().create(ApiInterface.class);
File file = FileUtils.getFile(getContext(), fileUri);
//Log.d("file", file.toString());
RequestBody requestFile = RequestBody.create(MediaType.parse("image/*"), file);
MultipartBody.Part body = MultipartBody.Part.createFormData("picture", file.getName(), requestFile);
Log.d("imageFile : ", requestFile.toString());
Call<UploadImageResponse> call = apiService.uploadImage(body);
call.enqueue(new Callback<UploadImageResponse>() {
@Override
public void onResponse(Call<UploadImageResponse> call, Response<UploadImageResponse> response) {
if(response.isSuccessful()) {
String getUrlResponse = response.body().getMessage();
Toast.makeText(getActivity(), "Data : " + getUrlResponse, Toast.LENGTH_SHORT).show();
}else{
Log.d("Message Response : ", response.body().getMessage());
Toast.makeText(getActivity(), "Something went wrong", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<UploadImageResponse> call, Throwable t) {
Toast.makeText(getActivity(), "Something went wrong", Toast.LENGTH_SHORT).show();
Log.e("Failure : ", String.valueOf(t));
}
});
}
这是我的apiInterface
@Multipart
@POST("v1/upload_doc")
Call<UploadImageResponse> uploadImage(@Part MultipartBody.Part image);