android无法压缩回收的位图错误

时间:2016-11-09 00:29:26

标签: android image bitmap android-asynctask

我正在尝试将图像上传到服务器,因此,我从图库中获取图像并将它们放在位图数组中,并将第一个图像保存在名为图标的单独位图中。在AsyncTask类中,我使用函数getResizedBitmap缩放大小超过2048 * 1152的图像然后我拿图标并尝试将其压缩到小尺寸但是在这里我得到以下错误并且此错误仅出现在我得到尺寸大于2048 * 1152的图像:

java.lang.IllegalStateException: Can't compress a recycled bitmap
                                                                                     at android.graphics.Bitmap.checkRecycled(Bitmap.java:400)
                                                                                     at android.graphics.Bitmap.compress(Bitmap.java:1310)
                                                                                     at com.example.user.animal_project.Main_Menu_frgments.ads_fragment.get_icon(ads_fragment.java:219)
                                                                                     at com.example.user.animal_project.Main_Menu_frgments.ads_fragment.access$700(ads_fragment.java:52)
                                                                                     at com.example.user.animal_project.Main_Menu_frgments.ads_fragment$Encode_image.onPostExecute(ads_fragment.java:210)
                                                                                     at com.example.user.animal_project.Main_Menu_frgments.ads_fragment$Encode_image.onPostExecute(ads_fragment.java:180)
                                                                                     at android.os.AsyncTask.finish(AsyncTask.java:651)
                                                                                     at android.os.AsyncTask.access$500(AsyncTask.java:180)
                                                                                     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:668)
                                                                                     at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                     at android.os.Looper.loop(Looper.java:148)
                                                                                     at android.app.ActivityThread.main(ActivityThread.java:7325)
                                                                                     at java.lang.reflect.Method.invoke(Native Method)
                                                                                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

这是我从画廊获取图片的地方:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {


if (requestCode == 10 && resultCode == Activity.RESULT_OK) {
    System.out.println("r111 is:"+data.toString());
    if(data!=null)
    {

        ClipData clipData = data.getClipData();


            icons=new Bitmap[1];
            bitmaps_group=new Bitmap[clipData.getItemCount()];
            files=new File[clipData.getItemCount()];
            for (int i = 0; i < clipData.getItemCount(); i++) {

                ClipData.Item item = clipData.getItemAt(i);
                Uri uri = item.getUri();

                try {
                    bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri);

                    bitmaps_group[i]=MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), uri);

                    if(i==0)
                    {
                        icons[0]=bitmaps_group[0];
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                }
                if(i==4)
                    i=clipData.getItemCount()+1;
            }
            new Encode_image().execute();
        }
       }

这里是AsyncTask类来检查图像的大小并调用使图标的函数:

private class Encode_image extends AsyncTask<Void,Void,Void> {

        @Override
        protected Void doInBackground(Void... voids) {
            ByteArrayOutputStream stream=new ByteArrayOutputStream();

            images_array=new String[bitmaps_group.length];
            for(int i=0;i<bitmaps_group.length;i++) {

                if(bitmaps_group[i].getHeight()>1160 ||bitmaps_group[i].getWidth()>2060)
                    try {
                        bitmaps_group[i]=getResizedBitmap(bitmaps_group[i],2048,1152);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                bitmaps_group[i].compress(Bitmap.CompressFormat.JPEG, 100, stream);
                byte[] array = stream.toByteArray();

                encoded_string = Base64.encodeToString(array, 0);
                images_array[i]=encoded_string;

                System.out.println("Respon1:" + bitmaps_group.length);
            }


            return null;
        }
        @Override
        protected void onPostExecute(Void aVoid) {
            icon_string=get_icon();
        }
    }

private String get_icon() {
        String encoding = null;
        ByteArrayOutputStream stream=new ByteArrayOutputStream();
        icons[0].compress(Bitmap.CompressFormat.JPEG, 40, stream);
        byte[] array = stream.toByteArray();

        encoded_string = Base64.encodeToString(array, 0);
        encoding=encoded_string;
        return encoding;
    }

这是我用来改变图像大小的函数:

 public Bitmap getResizedBitmap(Bitmap bm, int newWidth, int newHeight) throws IOException {
        int width = bm.getWidth();
        int height = bm.getHeight();
        int rotation=0;

        float scaleWidth = ((float) newWidth) / width;
        float scaleHeight = ((float) newHeight) / height;

        Matrix matrix=new Matrix();

        matrix.postScale(scaleWidth, scaleHeight);


        // "RECREATE" THE NEW BITMAP

        Bitmap resizedBitmap = Bitmap.createBitmap(
                bm, 0, 0, width, height, matrix,true);
        bm.recycle();
        return resizedBitmap;
    }

0 个答案:

没有答案