繁体   English   中英

Android:位图压缩导致内存不足问题

[英]Android: Bitmap compression is causing out of memory problems

我正在尝试压缩从图库中选取或由相机捕获的位图,然后再发送到服务器。 现在,我按照以下步骤进行操作:

1)获取图像的路径。

2)从该路径获取位图。

3)检查位图的初始宽度和高度,并将其与最大宽度和高度进行比较,然后使用比例因子调整宽度和高度。

4)使用exif检查并正确旋转位图。

5)最终降低质量以获得最终的压缩位图。

编码:

    private static final float max_width = 1280.0f;
    private static final float max_height = 1280.0f;
    private static final float max_ratio = max_width/max_height; 

    private void CompressImage(String path , Bitmap bitmap_original){



     //get width and height of original bitmap

     int original_width = bitmap_original.getWidth();
     int original_height = bitmap_origianl.getHeight();

     float original_ratio = (float)width/(float)height;

     if(original_height > max_height || original_width > max_width){
      //adjust bitmap dimensions

       int width = 0;
       int height = 0;

       if(original_ratio<max_ratio){
       width= (int)((max_height/original_height)*original_width);
       height=(int) max_height;  

       }else if(original_ratio>max_ratio){
        height= (int)((max_width/original_width)*original_height);
        width= (int)max_width;
       }else{
         height = max_height; 
         width = max_width;  
       }

       //adjust the bitmap 
        Bitmap adjusted_bitmap = Bitmap.createScaledBitmap(bitmap_original,width,height,false);


       //check rotation
       Matrix matrix = new Matrix();  
       try{
       ExifInterface exif = new ExifInterface(path);
       int original_orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION , ExifInterface.ORIENTATION_UNDEFINED);

         if(original_orientation == ExifInterface.ORIENTATION_ROTATE_90){

          matrix.setRotate(90);

         }else if(original_orientation == ExifInterface.ORIENTATION_180){

          matrix.setRotate(180);

         }else if(original_orientation == ExifInterface.ORIENTATION_270){

          matrix.setRotate(270);
         }

         Bitmap rotated_adjusted_bitmap = Bitmap.createBitmap(adjusted_bitmap , 0 , 0 , adjusted_bitmap.getWidth(), adjusted_bitmap.getHeight(),matrix,true);


         //lower the quality

          ByteArrayOutputStream out = new ByteArrayOutputStream();
          adjusted_rotated_bitmap.compress(Bitmap.CompressFormat.JPEG, 50, out);
          Bitmap adjusted_rotated_lowquality_bitmap = BitmapFactory.decodeStream(new ByteArrayInputStream(out.toByteArray()));  

       }catch(IOException e){


        }


      }else{
      //keep bitmap as is
      Bitmap adjusted_bitmap = bitmap_original;
      }



     }

问题

上面的所有方法都可以正常工作,但是问题是在某些情况下,应用程序会因内存不足异常而崩溃。

我的代码有问题吗?

我对位图和压缩不了解很多。

谢谢你的帮助。

位图通常需要时间,有时会阻塞I / O。 因此,在UI线程上调用imageWithText不是一个好习惯。

滑行被设计为具有灵活性,此问题很好地证明了该特性。

我强烈建议您使用下面的“滑行方式”。

ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
yourBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); 
Glide.with(this).load(stream.toByteArray())
     .asBitmap() .error(R.drawable.ic_thumb_placeholder) 
     .transform(new CircleTransform(this)).into(imageview);

有关更多详细信息,请查看下面的链接

有没有一种方法可以将图像作为位图加载到Glide

从图库中选择图像时,您可以获取图像uri:

Uri myUri = data.getData();

在将映像发送到服务器之前,您需要将该uri转换为byteArray []

如下:

    Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(),myUri);

   ByteArrayOutputStream baos = new ByteArrayOutputStream();

   bitmap.compress(Bitmap.CompressFormat.PNG,0,baos);
                        byteArray[] bArray = baos.toByteArray();
    If you want you can get the name of image as:
     try (Cursor returnCursor = getContentResolver().query(myUri, null, null, null, null)) {
                        int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
                        int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
                        returnCursor.moveToFirst();
                           String FileName = returnCursor.getString(nameIndex);}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM