在 Android 中处理大图像
我在 android 中有一个应用程序,其中我正在处理非常大的图像(640x480)
和稍大的图像。这实际上是用相机拍摄的照片,然后进行编辑,然后保存到 sdcard
最后上传到服务器。 但我面临的问题是使用位图
时虚拟机内存超出
。
我正在做这样的事情:
在第一个活动中,我接收来自相机的字节
并创建一个位图
,对其进行编辑,然后保存到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;
在我的第二个活动中,我从 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));
仍然没有任何结果......同样的错误......内存不足
。
有人有什么想法吗?
PS:我也尝试调用 System.gc()
但结果相同!
所以请帮助我!
I have an app in android in which I'm working with really big images(640x480)
and slightly bigger.This are actually pictures taken with the camera, then are edited, after that are saved to sdcard
and finally uploaded to a server.
But the issue that I'm facing is VM memory exceeded
when working with bitmaps
.
I'm doing something like this:
In the first activity I'm receiving the bytes from the camera
and create a bitmap
which is edited and then saved to 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);
in onPause()
method I did this:
bitmapResult.recycle();
bitmapResult=null;
In my second activity I'm reading the file from sdcard
and display it on the screen.Why?I have my reasons:
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);
}
And did the same thing in onPause()
x.recycle();
x=null;
All this didn't worked and after taking a few pictures my app crashed.
I tried using WeaakReference
instead of bitmap
:
myImage = new WeakReference<Bitmap>(BitmapFactory.decodeByteArray(imageData, 0,
imageData.length, options));
And still nothing....the same error....out of memory
.
Has anyone any idea?
P.S: I also tried to call System.gc()
but with the same result!
So please help me!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用以下代码调整图像大小
`
`
Use the below code to resize the image
`
`