为了账号安全,请及时绑定邮箱和手机立即绑定

将图片插入图像视图

将图片插入图像视图

收到一只叮咚 2022-07-14 09:43:55
在我的应用程序中,我将两张图片插入到两个图像视图中,并使用活动结果从图库中获取照片。 private void showFileChooser () {        mHandler.post(new Runnable() {            @Override            public void run() {                Intent intent = new Intent();                intent.setType("image/*");                intent.setAction(Intent.ACTION_GET_CONTENT);                startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);            }        });}private void showFileChooser2 () {    mHandler.post(new Runnable() {        @Override        public void run() {            Intent intent2 = new Intent();            intent2.setType("image/*");            intent2.setAction(Intent.ACTION_GET_CONTENT);            startActivityForResult(Intent.createChooser(intent2, "Select Picture"), PICK_IMAGE_REQUEST2);        }    });} @Override        protected void onActivityResult ( int requestCode, int resultCode, Intent data){            super.onActivityResult(requestCode, resultCode, data);            if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {                Uri filePath = data.getData();                try {                    //Getting the Bitmap from Gallery                    bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);                    rbitmap = getResizedBitmap(bitmap, 1000);//Setting the Bitmap to ImageView                    imageViewUserImage.setImageBitmap(rbitmap);                    imageViewUserImage.requestFocus();                } catch (IOException e) {                    e.printStackTrace();                }                }}}该应用程序运行良好,但有时会发生奇怪的事情。有时,一旦我在图库中单击所需的照片,该应用程序就会返回主活动,并且我发现另一个图像视图中先前加载的图像已被删除。换句话说,有时在其中一个中加载图片会删除另一个中加载的图像。这种故障并不总是发生,它有时会发生,有时应用程序运行良好,没有任何问题。我该如何解决?
查看完整描述

2 回答

?
慕容森

TA贡献1853条经验 获得超18个赞

在 'e.printStackTrace();' 的 catch 处放置一个断点 线。玩应用程序,看看失败的原因。没有任何堆栈跟踪,我们只能猜测原因。



查看完整回答
反对 回复 2022-07-14
?
慕尼黑5688855

TA贡献1848条经验 获得超2个赞

我发现了问题。图像的大小有点大,所以会出现“内存不足”错误。为了避免这样的问题,我在 if 情况下回收了每个位图。


private void showFileChooser () {


        mHandler.post(new Runnable() {

            @Override

            public void run() {


                Intent intent = new Intent();

                intent.setType("image/*");

                intent.setAction(Intent.ACTION_GET_CONTENT);

                startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);

            }

        });

}


private void showFileChooser2 () {


    mHandler.post(new Runnable() {

        @Override

        public void run() {


            Intent intent2 = new Intent();

            intent2.setType("image/*");

            intent2.setAction(Intent.ACTION_GET_CONTENT);

            startActivityForResult(Intent.createChooser(intent2, "Select Picture"), PICK_IMAGE_REQUEST2);


        }

    });

}



 @Override

        protected void onActivityResult ( int requestCode, int resultCode, Intent data){

            super.onActivityResult(requestCode, resultCode, data);


            if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {

                Uri filePath = data.getData();

                try {

                    //Getting the Bitmap from Gallery

                    bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);

                    rbitmap = getResizedBitmap(bitmap, 1000);//Setting the Bitmap to ImageView

                    imageViewUserImage.setImageBitmap(rbitmap);

                    bitmap.recycle;

                    imageViewUserImage.requestFocus();

                } catch (IOException e) {

                    e.printStackTrace();

                }

            }else if (requestCode == PICK_IMAGE_REQUEST2 && resultCode == RESULT_OK && data != null && data.getData() != null) {

                Uri filePath2 = data.getData();

                try {

                    //Getting the Bitmap from Gallery

                    bitmap2 = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath2);

                    rbitmap2 = getResizedBitmap(bitmap2, 1000);//Setting the Bitmap to ImageView

                    imageViewUserImage2.setImageBitmap(rbitmap2);

                    bitmap2.recycle;

                    imageViewUserImage2.requestFocus();

                } catch (IOException e) {

                    e.printStackTrace();

                }

}

}



查看完整回答
反对 回复 2022-07-14
  • 2 回答
  • 0 关注
  • 83 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信