【发布时间】:2016-09-10 05:05:28
【问题描述】:
我正在尝试使用 Volley 使用图像和其他详细信息更新个人资料。
我必须转换图像文件并将其作为参数发送到 url。
我还必须解析所有其他详细信息以更新配置文件。
如何更新个人资料图片?
url = "http://192.168.1.30:1021/mobileapi/profile_update.html?&user_id="+user+"&firstname="+First.getText().toString()+
"&lastname="+last.getText().toString()+"&secondaryemail="+sEmail.getText().toString()+
"&phonemobile="+mob.getText().toString()+"&aboutus="+work.getText().toString()+"&phonework="+phone.getText().toString()+
"&skypeid="+skypeid.getText().toString()+"&gender="+gender+"&smtp_password="+mpass.getText().toString()+"&image="+fos;
url=url.trim().replaceAll("\\s+","");
System.out.println("url" + url);
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading...");
pDialog.show();
pDialog.dismiss();
queue = Volley.newRequestQueue(getActivity());
request = new JsonArrayRequest(Request.Method.POST, url, new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
System.out.println("updaa" + response.toString());
JSONObject update = response.getJSONObject(0);
int httpcode = Integer.parseInt(update.getString("status"));
if (httpcode == 200) {
String mess = update.getString("message");
Toast.makeText(getActivity(), mess, Toast.LENGTH_LONG).show();
int userid = Integer.parseInt(update.getString("user_id"));
String fname = update.getString("firstname");
String lname = update.getString("lastname");
String mail = update.getString("email");
String smail=update.getString("secondary_email");
/* String ph= String.valueOf(Integer.parseInt(update.getString("phone_work")));
String phone= String.valueOf(Integer.parseInt(update.getString("phone_mobile")));*/
String skype_id = update.getString("skype_id");
String user_title = update.getString("user_title");
String imgUrl = update.getString("image");
editor.putString("name", fname);
editor.putString("userid", String.valueOf(userid));
editor.putString("lname",lname);
editor.putString("mail",mail);
editor.putString("skype",skype_id);
editor.putString("image",imgUrl);
// editor.putString("phone", String.valueOf(ph));
editor.putString("work",user_title);
editor.putString("smail",smail);
// editor.putString("phone", String.valueOf(phone));
editor.commit();
} else {
JSONObject typ = response.getJSONObject(0);
String message = typ.getString("message");
Toast.makeText(getActivity(), message, Toast.LENGTH_LONG).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
System.out.println("result" + error.getLocalizedMessage()+"__"+error.getMessage());
Log.e("Volley", "Error");
}
}){
};
queue.add(request);
}
}
});
return view;
}
private void opengallery() {
Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == PICK_IMAGE_REQUEST && intent != null && intent.getData() != null) {
Uri uri = intent.getData();
try {
/*Uri selectedImageURI = data.getData();
imageFile = new File(getRealPathFromURI(selectedImageURI));*/
Bitmap bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri);
Log.d("TAG", String.valueOf(bitmap));
Bpro.setImageBitmap(bitmap);
String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
OutputStream outStream = null;
File file = new File(uri + ".png");
file = new File(extStorageDirectory, uri + ".png");
Log.e("file exist", "" + file + ",Bitmap= " + uri);
fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 85, fos);
/* fos = new FileOutputStream(f);
fos.write(bitmapdata);*/
} catch (IOException e) {
e.printStackTrace();
}
}
}
JSON 响应
[{
"smtp_password":"abdul",
"status":"200",
"user_type":"2",
"skype_id":"skype",
"emp_id":"N0418",
"phone_work":"9864545454",
"image":"http:\/\/192.168.1.30:1021\/uploads\/Nandha\/profile_image\/180_200\/45571.png",
"lastname":"Rahman",
"firstname":"Abdul",
"user_title":"android",
"message":"Profile has been updated successfully",
"email":"4dZR9RLF@gmail.com",
"gender":"1",
"user_id":"45571",
"secondary_email":"abdul@gmail.com",
"phone_mobile":"9892545454"
}]
【问题讨论】:
-
有什么问题?同时显示您的 JSON 响应。
-
urlhttp://192.168.1.30:1021/mobileapi/profile_update.html?&user_id=45571&firstname=Abdul&lastname=Rahman&secondaryemail=abdul@gmail.com&phonemobile=9892545454&aboutus=android&phonework=9864545454&skypeid=skype&gender=1 null 即使从图库中选择图像,我也将图像设为 null
-
我必须更新个人资料图片以及如何将图片作为文件发送
-
我强烈建议使用
Uri.Builder作为该 url 字符串,而不是所有字符串连接
标签: android file android-volley multipart