java - 如何使用 CircleImageView Android Studio 保存图像
问题描述
我编写了一个程序,用户可以单击 TextView(editPhoto)并选择图像(个人资料图片)并显示在个人资料上,并将其与其他个人资料详细信息一起保存在我的数据库中,但只要我离开页面图片从 CircleImageView 中消失,尽管它仍然保存在我的数据库中。我可以使用 .setText() 将其他用户信息保存在个人资料上,例如姓名、用户名、电子邮件等,那么如何使用 CircleImageView 对个人资料图片执行相同操作?提前致谢
代码:
private TextView editPhoto;
CircleImageView profilePic;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_edit_profile);
profilePic = findViewById(R.id.profilepic);
editPhoto = findViewById(R.id.txtEditPP);
editPhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
chooseFile();
}
});
}
private void chooseFile(){
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Choose Photo"),1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1 && resultCode == RESULT_OK && data != null && data.getData() != null){
Uri filepath = data.getData();
try {
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filepath);
profilePic.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
UploadPicture(getId, getStringImage(bitmap));
}
}
//UPLOAD PROFILE PICTURE
private void UploadPicture(final String id, final String photo) {
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_UPLOAD, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.i(TAG, response.toString());
try {
JSONObject jsonObject = new JSONObject(response);
String success = jsonObject.getString("success");
if (success.equals("1")){
Toast.makeText(EditProfile.this, "Photo Uploaded", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(EditProfile.this, "Error Uploading Image!", Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(EditProfile.this, "Error Uploading Image!", Toast.LENGTH_SHORT).show();
}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("ID", id);
params.put("photo", photo);
return params;
}
};
RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
public String getStringImage(Bitmap bitmap){
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, byteArrayOutputStream);
byte[] imageByteArr = byteArrayOutputStream.toByteArray();
String encodedImage = Base64.encodeToString(imageByteArr, Base64.DEFAULT);
return encodedImage;
}
解决方案
在 onResume 中,您可以使用位图对象重新加载 imageView 吗?在这种情况下,还将位图对象作为类变量重新加载图像对您有用。
推荐阅读
- angular - 在 Angular 6 中读取文件的扩展名
- c - 解锁内核线程的正确方法
- api - 如何使用 API 将照片与表单数据一起上传到后端服务器?
- java - 如何修复错误:无法从“11.0.2”确定 java 版本。在特拉维斯?
- arrays - 使用 sqlite 数据库将字符串转换为 json 数组
- android - 在 android 项目中使用 Actions.xml
- sql - 删除给定日期中的重复行组合
- ios - 将字符串快速转换为日期后未显示日期
- c++ - shared_ptr C++ 可能存在内存泄漏
- javascript - Node.js 与 Express 的关系