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的解决方案,但我的位图是可用的,因此如何使用此照片直接。
您好,感谢您的回复,但我不知道如何通过我的位图功能,decodeSampledBitmapFromResource,因为它需要的资源,我有已经位图available.how将我的位图传递给它? –
如何将位图存储在Global.photo_editor_bitmap中?因为你将这个位图作为byteArray发送给另一个活动 – Pankaj