亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將圖片插入圖像視圖

將圖片插入圖像視圖

收到一只叮咚 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 關注
  • 123 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號