gpt4 book ai didi

android - 在 android 中处理大图像

转载 作者:太空宇宙 更新时间:2023-11-03 11:21:42 25 4
gpt4 key购买 nike

我在 android 中有一个应用程序,我在其中处理非常大的图像(640x480) 并且稍微大一点。这实际上是用相机拍摄的照片,然后进行编辑,然后保存到sdcard 最后上传到服务器。 但我面临的问题是在使用 bitmapsVM memory exceeded

我正在做这样的事情:

在第一个 Activity 中,我从相机接收字节并创建一个位图,编辑后保存到sdcard

 BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
options.inDither = true;
byte[] imageData = extras.getByteArray("imageData");
myImage = BitmapFactory.decodeByteArray(imageData, 0,
imageData.length, options);

Matrix mat = new Matrix();
mat.postRotate(90);

if(myImage !=null){
bitmapResult = Bitmap.createBitmap(myImage.get(), 0, 0, (myImage.get()).getWidth(),
(myImage.get()).getHeight(), mat, true);

onPause() 方法中我这样做了:

 bitmapResult.recycle();
bitmapResult=null;

在我的第二个 Activity 中,我从 sdcard 读取文件并将其显示在屏幕上。为什么?我有我的理由:

File f=new File("/sdcard/Images/android.jpg");  
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inTempStorage = new byte[16*1024];
o2.inSampleSize=8;
o2.outWidth=100;
o2.outHeight=100;

try {
x = BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

if(x != null){

myImage.setImageBitmap(x);
}

并在 onPause()

中做了同样的事情
x.recycle();
x=null;

所有这一切都不起作用,拍了几张照片后我的应用程序崩溃了。

我尝试使用 WeaakReference 而不是 bitmap:

myImage = new WeakReference<Bitmap>(BitmapFactory.decodeByteArray(imageData, 0,
imageData.length, options));

仍然没有....同样的错误....内存不足

有人知道吗?

P.S: 我也尝试调用 System.gc() 但结果相同!

所以请帮助我!

最佳答案

使用下面的代码调整图片大小

`

Bitmap bmp = BitmapFactory.decodeFile(imageFilePath);
int width = bmp.getWidth();
int height = bmp.getHeight();
float scaleWidth = ((float) 300) / width;
float scaleHeight = ((float) 300) / height;
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
Bitmap resizedBitmap = Bitmap.createBitmap(bmp, 0, 0, width,
height, matrix, true);
ByteArrayOutputStream baostream = new ByteArrayOutputStream();
resizedBitmap.compress(Bitmap.CompressFormat.PNG, 100, baostream);

`

关于android - 在 android 中处理大图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7305490/

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