将图片插入图像视图

在我的应用程序中,我将两张图片插入到两个图像视图中,并使用活动结果从图库中获取照片。


 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();

                }

                }

}

}

该应用程序运行良好,但有时会发生奇怪的事情。有时,一旦我在图库中单击所需的照片,该应用程序就会返回主活动,并且我发现另一个图像视图中先前加载的图像已被删除。换句话说,有时在其中一个中加载图片会删除另一个中加载的图像。这种故障并不总是发生,它有时会发生,有时应用程序运行良好,没有任何问题。


我该如何解决?


收到一只叮咚
浏览 84回答 2
2回答

慕容森

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

慕尼黑5688855

我发现了问题。图像的大小有点大,所以会出现“内存不足”错误。为了避免这样的问题,我在 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();                }}}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java