首页 > 解决方案 > 如何使用 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;
    }

标签: javaandroid

解决方案


在 onResume 中,您可以使用位图对象重新加载 imageView 吗?在这种情况下,还将位图对象作为类变量重新加载图像对您有用。


推荐阅读