0
我试图将图像发送到我的服务器,我发现使用TypedFile许多解决方案,但现在已经过时,我已经使用RequestBody尝试,但我JSON似乎是空,使用Django的作为后端架构和IM这里是我的改造代码:发送JPG到服务器与改造
EndpointInterface service = ServiceAuthGenerator.createService(EndpointInterface.class);
MediaType mediaType = MediaType.parse("multipart/form-data; boundary=---011000010111000001101001");
RequestBody body = RequestBody.create(mediaType,file);
//RequestBody fileBody = RequestBody.create(MediaType.parse("image/jpg"), file);
Call<JSONObject> call = service.uploadPhoto(body);
call.enqueue(new Callback<JSONObject>() {
@Override
public void onResponse(Response<JSONObject> response, Retrofit retrofit) {
Log.v("Upload->", response.message().toString());
}
@Override
public void onFailure(Throwable t) {
Log.e("Upload", t.getMessage());
}
});
}
谁能告诉我什么即时做错了或者我可怎么办呢?谢谢! :d
编辑:
这里是我的活动功能:
public void sendPhoto(File file)
{
EndpointInterface service = ServiceAuthGenerator.createService(EndpointInterface.class);
RequestBody fbody = RequestBody.create(MediaType.parse("image/*"), file);
RequestBody namebody = RequestBody.create(MediaType.parse("text/plain"), "735064");
Call<JSONObject> call = service.uploadPhoto(fbody,namebody);
call.enqueue(new Callback<JSONObject>() {
@Override
public void onResponse(Response<JSONObject> response, Retrofit retrofit) {
Log.v("Upload->", response.message().toString());
}
@Override
public void onFailure(Throwable t) {
Log.e("Upload", t.getMessage());
}
});
}
,这是我的图片设置为我的ImageView:
builder.setTitle("Profile Photo");
builder.setIcon(R.drawable.gallery2);
builder.setAdapter(adapter,new DialogInterface.OnClickListener()
{
public void onClick (DialogInterface dialog,int item){
if (item == 0) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
/*file = new File(Environment.getExternalStorageDirectory(),
"tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".png");*/
file = new File(Environment.getExternalStorageDirectory(),"tmp_avatar.jpg");
mImageCaptureUri = Uri.fromFile(file);
try {
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);
intent.putExtra("return-data", true);
startActivityForResult(intent, PICK_FROM_CAMERA);
} catch (Exception e) {
e.printStackTrace();
}
dialog.cancel();
} else {
Intent intent = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
intent.setType("image/*");
startActivityForResult(Intent.createChooser(intent, "Complete action using"), PICK_FROM_FILE);
}
}
}
);
final AlertDialog dialog = builder.create();
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) return;
Bitmap bitmap = null;
if (requestCode == PICK_FROM_FILE) {
mImageCaptureUri = data.getData();
path = getRealPathFromURI(mImageCaptureUri); //from Gallery
if (path == null)
path = mImageCaptureUri.getPath(); //from File Manager*/
if (path != null)
bitmap = BitmapFactory.decodeFile(path);
} else {
path = mImageCaptureUri.getPath();
bitmap = BitmapFactory.decodeFile(path);
}
final double viewWidthToBitmapWidthRatio = (double) mImageView.getWidth()/(double) bitmap.getWidth();
mImageView.getLayoutParams().height = (int) (bitmap.getHeight() * viewWidthToBitmapWidthRatio);
mImageView.setImageBitmap(bitmap);
}
public String getRealPathFromURI(Uri contentUri) {
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(contentUri, proj, null, null, null);
if (cursor == null) {
return null;
}
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
谢谢您的回答!但我有一个问题,在文件名= /(这必须是相同的图像)?或者我可以在那里设置名称? –
我们需要对文件名进行硬编码,以便您的后端能够识别出有文件作为进一步处理的有效载荷。 https://futurestud.io/blog/retrofit-2-how-to-upload-files-to-server –
同时检查https://github.com/square/retrofit/issues/1063了解更多详情 –