如何使用MULTIPART/FORM-DATA上传用户的个人资料图片? [英] How to upload profile pic of a user using MULTIPART/FORM-DATA?
问题描述
我正在尝试使用MULTIPART/FORM-DATAg上传图像(从移动设备中选择),由于我是Multipart的新手,所以无法弄清楚. 要上传的数据是图像(不是AS BASE64CODE)和用户ID.
I'm trying to upload an image (which is selected from mobile ) using MULTIPART/FORM-DATAg and couldn't figure it out as I'm a newbie to Multipart. The data that is to be Uploaded are the image(NOT AS BASE64CODE) and user id.
推荐答案
以下是使用Multipart上传图像的解决方案
Here is the solution for uploading Image using Multipart
将此文件添加到依赖项
Add this file to dependencies
//对于多部分
实现'com.karumi:dexter:5.0.0'
implementation 'com.karumi:dexter:5.0.0'
现在在Activity类中,声明这2个字段
Now in Activity class, declare this 2 field
private RequestQueue rQueue;
Uri file_uri_1;
下一步是从链接
现在创建一个函数来上传您的Imageview
Now Create a function to upload your Imageview
private void uploadImage() {
ProgressDialog dialog = new ProgressDialog(getActivity());
dialog.setCancelable(false);
dialog.setMessage("Please wait...");
dialog.show();
MultipartRequest volleyMultipartRequest = new MultipartRequest(Request.Method.POST, <URL API URL>,
new Response.Listener<NetworkResponse>() {
@Override
public void onResponse(NetworkResponse response) {
dialog.dismiss();
rQueue.getCache().clear();
try {
JSONObject jsonObject = new JSONObject(new String(response.data));
<GET YOUR RESPONSE FROM SEVER HERE>
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
dialog.dismiss();
Toast.makeText(getActivity(), error.getMessage(), Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
// Add parameter if you wish send extra parameters with Image
params.put("<KEY>", <VALUE>);
return params;
}
@Override
protected Map<String, DataPart> getByteData() {
Map<String, DataPart> params = new HashMap<>();
// Check your file name or path is Empty or not
if (fileName != null && !fileName.equalsIgnoreCase("")) {
File file = new File(filePath);
file_uri_1 = Uri.fromFile(file);
params.put("<IMAGE KEY>", new DataPart(fileName, getFileDataFromDrawable(getActivity(), file_uri_1), getFileType(file_uri_1)));
}
return params;
}
};
volleyMultipartRequest.setRetryPolicy(new DefaultRetryPolicy(0, DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
rQueue = Volley.newRequestQueue(getActivity());
rQueue.add(volleyMultipartRequest);
}
public static byte[] getFileDataFromDrawable(Context context, Uri uri) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try {
InputStream iStream = context.getContentResolver().openInputStream(uri);
int bufferSize = 2048;
byte[] buffer = new byte[bufferSize];
// we need to know how may bytes were read to write them to the byteBuffer
int len = 0;
if (iStream != null) {
while ((len = iStream.read(buffer)) != -1) {
byteArrayOutputStream.write(buffer, 0, len);
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
return byteArrayOutputStream.toByteArray();
}
希望此代码对您有帮助.
Hope this code will help you.
这篇关于如何使用MULTIPART/FORM-DATA上传用户的个人资料图片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!