在 Android 中处理大图像

发布于 2024-12-03 11:23:33 字数 2093 浏览 0 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我一直都在从未离去 2024-12-10 11:23:33

使用以下代码调整图像大小

`

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

`

Use the below code to resize the image

`

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

`

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文