gpt4 book ai didi

java - 4320016 字节分配内存不足

转载 作者:行者123 更新时间:2023-12-01 17:01:18 24 4
gpt4 key购买 nike

enter image description here我不知道发生了什么,一开始它正在运行我有这个错误,这是我的代码:

我不知道发生了什么,一开始它正在运行我有这个错误,这是我的代码:

我不知道发生了什么,一开始它正在运行我有这个错误,这是我的代码:

    public void allFile() {
mTempDir = Environment.getExternalStorageDirectory()
+ "/MyCameraBooth/Photo/";

File mTempFile = new File(mTempDir);
if (!mTempFile.exists()) {
mTempFile.mkdirs();
}

mBackground = Bitmap.createBitmap(1800, 1200, Bitmap.Config.RGB_565);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;

File f = new File(AppConstant.filepathone);
Bitmap mImageone = decodeFile(f);
File g = new File(AppConstant.filepathtwo);
Bitmap mImagetwo = decodeFile(g);
File h = new File(AppConstant.filepaththree);
Bitmap mImagethree = decodeFile(h);
File i = new File(AppConstant.filepathfour);
Bitmap mImagefour = decodeFile(i);

Map<String, Bitmap> mBgMap = new HashMap<String, Bitmap>();
mBgMap.put("Frame1", BitmapFactory.decodeResource(getResources(),
R.drawable.frameone));
mBgMap.put("Frame2", BitmapFactory.decodeResource(getResources(),
R.drawable.frametwo));
mBgMap.put("Frame3", BitmapFactory.decodeResource(getResources(),
R.drawable.framethree));
mBg = mBgMap.get(sharedpreferences.getString("getFrame", "getFrame"));

Bitmap mBack = Bitmap.createScaledBitmap(mBg, 1800, 1200, true);
Bitmap mImaget = Bitmap.createScaledBitmap(mImagetwo, 515, 360, true);
Bitmap mImageth = Bitmap
.createScaledBitmap(mImagethree, 515, 360, true);
Bitmap mImagef = Bitmap.createScaledBitmap(mImagefour, 515, 360, true);
Bitmap mImageo = Bitmap.createScaledBitmap(mImageone, 1080, 635, true);

mCanvas = new Canvas(mBackground);
mCanvas.drawARGB(255, 150, 150, 10);
mCanvas.drawBitmap(mBack, 0, 0, null);
mCanvas.drawBitmap(mImaget, 75, 75, null);
mCanvas.drawBitmap(mImageo, 75, 490, null);
mCanvas.drawBitmap(mImageth, 645, 75, null);
mCanvas.drawBitmap(mImagef, 1215, 75, null);

try {
String friendlydate = DateFormat.getTimeInstance(DateFormat.MEDIUM)
.format(new Date());
friendlydate = friendlydate.replace(':', '_');
String filename = friendlydate + ".jpg";
mBitmapDrawable = new BitmapDrawable(mBackground);
Bitmap mNewSaving = mBitmapDrawable.getBitmap();
String FtoSave = mTempDir + filename;
File mFile = new File(FtoSave);
mFileOutputStream = new FileOutputStream(mFile);
mNewSaving.compress(Bitmap.CompressFormat.PNG, 100,
mFileOutputStream);

imVCature_pic.setImageBitmap(decodeSampledBitmapFromFile(
mFile.getAbsolutePath(), 1800, 1200));

// imVCature_pic.setImageBitmap(decodeFile(mFile));
mFileOutputStream.flush();
mFileOutputStream.close();
mFileOutputStream = null;

} catch (FileNotFoundException e) {
Log.e(AppConstant.TAG, "FileNotFoundExceptionError " + e.toString());
} catch (IOException e) {
Log.e(AppConstant.TAG, "IOExceptionError " + e.toString());
}
Log.i(AppConstant.TAG, "Image Created");

}


private static Bitmap decodeFile(File f) {
try {
// Decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f), null, o);

// The new size we want to scale to
final int REQUIRED_SIZE = 150;

// Find the correct scale value. It should be the power of 2.
int scale = 1;
while (o.outWidth / scale / 2 >= REQUIRED_SIZE
&& o.outHeight / scale / 2 >= REQUIRED_SIZE)
scale *= 2;

// Decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
o.inJustDecodeBounds = false;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {

}

return null;
}

最佳答案

这是一个典型的 Android 问题。当您尝试为图像分配内存时,内存不足。您可以尝试两种方法来解决此问题。

快速方法:在应用程序中启用大堆,只需将 android:largeHeap="true" 添加到 android-manifest.xml 内的应用程序标记即可。这种方法的问题是大堆不适用于较旧的 Android 版本,如果我没记错的话,您至少需要 Android 3.0 才能完成此工作。

http://developer.android.com/guide/topics/manifest/application-element.html

好方法:将缩小版本加载到内存中。您可以检查此链接以了解如何执行此操作 http://developer.android.com/training/displaying-bitmaps/load-bitmap.html#load-bitmap

但主要思想在于这段代码:

mImageView.setImageBitmap(
decodeSampledBitmapFromResource(getResources(), R.id.myimage, 100, 100));

public static int calculateInSampleSize(
BitmapFactory.Options options, int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;

if (height > reqHeight || width > reqWidth) {

final int halfHeight = height / 2;
final int halfWidth = width / 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;
}

public 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);
}

关于java - 4320016 字节分配内存不足,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27547942/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com