2015-05-02 31 views
1

我做了一个图像编辑的演示,当我将图像传递给另一个活动(增大堆大小)时,我遇到了问题,并且我的应用程序崩溃了,请告诉我Cani如何直接采样和调整位图的大小(不是来自资源)解决内存问题,我的代码如下: 代码如何在android中对内存管理位图进行采样?

if (Global.photo_editor_bitmap != null) { 
       ByteArrayOutputStream stream = new ByteArrayOutputStream(); 
       Global.photo_editor_bitmap.compress(Bitmap.CompressFormat.PNG, 
         100, stream); 
       byte[] byteArray = stream.toByteArray(); 

       i = new Intent(PhotoEditor.this, Enhance.class); 
       i.putExtra("bitmap_image", byteArray); 
       i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); 
       i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 
       startActivity(i); 
      } else { 

       Toast.makeText(getApplicationContext(), "No Image", 1).show(); 
      } 

    static int calculateInSampleSize(BitmapFactory.Options options, 
      int reqWidth, int reqHeight) { 
     int inSampleSize = 1; // Default subsampling size 
     // See if image raw height and width is bigger than that of required 
     // view 
     if (options.outHeight > reqHeight || options.outWidth > reqWidth) { 
      // bigger 
      final int halfHeight = options.outHeight/2; 
      final int halfWidth = options.outWidth/2; 
      // Calculate the largest inSampleSize value that is a power of 2 and 
      // keeps both 
      // height and width larger than the requested height and width. 
      while ((halfHeight/inSampleSize) > reqHeight 
        && (halfWidth/inSampleSize) > reqWidth) { 
       inSampleSize *= 2; 
      } 
     } 
     return inSampleSize; 
    } 

    static Bitmap decodeSampledBitmapFromResource(Resources res, int resId, 
      int reqWidth, int reqHeight) { 
     // First decode with inJustDecodeBounds=true to check dimensions 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeResource(res, resId, options); 
     // Calculate inSampleSize 
     options.inSampleSize = calculateInSampleSize(options, reqWidth, 
       reqHeight); 
     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 
     return BitmapFactory.decodeResource(res, resId, options); 
    } 

我decodebitmapFromresources的解决方案,但我的位图是可用的,因此如何使用此照片直接。

回答

0

我有同样的问题应用程序崩溃,并在日志中显示消息堆大小,分配大小和位图大小,我所做的是检查设备宽度&高度和基于该比例缩放位图或计算样本大小。

int screenHeight = getResources().getDisplayMetrics().heightPixels; 
int screenWidth = getResources().getDisplayMetrics().widthPixels; 

得到的宽度和高度,然后把它放在reqWidth和reqHeight

+0

您好,感谢您的回复,但我不知道如何通过我的位图功能,decodeSampledBitmapFromResource,因为它需要的资源,我有已经位图available.how将我的位图传递给它? –

+0

如何将位图存储在Global.photo_editor_bitmap中?因为你将这个位图作为byteArray发送给另一个活动 – Pankaj