本文介绍了Android应用程序崩溃的三星Galaxy S3(内存不足错误)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Android应用程序,需要一些图片,重新大小他们,送他们转移到后端服务器。这个程序工作完全正常的所有​​其他手机(姜饼和冰淇淋三明治),除了三星Galaxy S3。每当需要的图片,并试图重新大小,它耗尽内存。起初,我认为这是一个问题的大小调整的一部分,我实现与insamplesize位图对象,并尝试了一切,但它仍然保留在崩溃。然后,我意识到,当应用程序启动的第一,它使用了大量的内存。在HTC和所有其他的手机,当我的应用程序启动时,它使用了大约4 MB,但是在Galaxy S3,它使用了约30 MG - 近10倍。我似乎无法找出什么会导致它使用了这么多的内存。我回收所有对象和布局得当(我认为)。这里是code我要调整图像大小:

I have an Android app that takes some pictures, re-sizes them and sends them over to the back-end server. This app works perfectly fine on all other phones (Gingerbread and Ice cream sandwich) except Samsung Galaxy S3. Whenever it takes the pictures and tries to re-size it, it runs out of memory. Initially I thought it was an issue with the re-sizing part and I implemented the bitmap object with insamplesize and tried everything but it still kept on crashing. Then I realized that when the app launches at first, it is using up a lot of memory. On HTC and all other phones, when my app launches, it uses up about 4 MB but on the Galaxy S3, it uses up about 30 MG - almost 10 times more. I can't seem to figure out what would cause it to use up that much more memory. I am recycling all the objects and layouts properly (I think). Here is the code I have to resize the image:

public static void resizeImage(String pathOfInputImage,
        String pathOfOutputImage) {
    try {
        int inWidth = 0;
        int inHeight = 0;
        float factor;

        InputStream in = new FileInputStream(pathOfInputImage);

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeStream(in, null, options);
        inWidth = options.outWidth;
        inHeight = options.outHeight;

        in.close();
        in = null;

        options = null;

        if (inWidth > inHeight) {
            factor = (float) inHeight / 480;
        } else {
            factor = (float) inWidth / 480;
        }
        options = new BitmapFactory.Options();

        Bitmap roughBitmap;

        if(Build.VERSION.SDK_INT < 12) {
            System.gc();
        }

        try
        {
            in = new FileInputStream(pathOfInputImage);
            options.inSampleSize = 4;
            roughBitmap = BitmapFactory.decodeStream(in, null, options);
        }
        catch (OutOfMemoryError e)
        {
                roughBitmap = Utils.fetchRoughBitmap(pathOfInputImage, factor);
            }
        finally
        {
            roughBitmap = Utils.fetchRoughBitmap(pathOfInputImage, factor);
            }

        in.close();
        in = null;

        boolean rotate = false;
        Bitmap rotatedBitmap = null;

        if (isNeedToRotate(pathOfInputImage)) {
            rotate = true;
            Matrix m = new Matrix();

            m.postRotate(90);
            try
            {
                rotatedBitmap = Bitmap.createBitmap(roughBitmap, 0, 0,
                        roughBitmap.getWidth(), roughBitmap.getHeight(), m,
                        true);
            }
            catch (OutOfMemoryError e)
            {
                rotatedBitmap = Bitmap.createBitmap(roughBitmap, 0, 0,
                        roughBitmap.getWidth()/2, roughBitmap.getHeight()/2, m,
                        true);
            }
        }


        if (rotate) {
            Utils.log(TAG, "Rotate Invoked :------->");
            int temp = inHeight;

            inHeight = inWidth;
            inWidth = temp;
        }

        options.inSampleSize = Math.round(factor);

        float dstWidth = (int) inWidth / factor;
        float dstHeight = (int) inHeight / factor;

        Bitmap resizedBitmap;
        if (rotate) {
            try
            {
                resizedBitmap = Bitmap.createScaledBitmap(rotatedBitmap,
                        (int) dstWidth, (int) dstHeight, true);
            }
            catch (OutOfMemoryError e)
            {
                resizedBitmap = Bitmap.createScaledBitmap(rotatedBitmap,
                        (int) dstWidth/2, (int) dstHeight/2, true);
            }

        } else {

            try
            {
                resizedBitmap = Bitmap.createScaledBitmap(roughBitmap,
                    (int) dstWidth, (int) dstHeight, true);
            }
            catch (OutOfMemoryError e)
            {
                resizedBitmap = Bitmap.createScaledBitmap(roughBitmap,
                        (int) dstWidth/2, (int) dstHeight/2, true);
            }
        }

        try
        {
            FileOutputStream out = new FileOutputStream(pathOfOutputImage);
            resizedBitmap.compress(Bitmap.CompressFormat.PNG, 80, out);
        }
        catch (Exception e) {
            Utils.log("Image", e.getMessage() + e);
        }

        //Free up the memory.
        roughBitmap.recycle();
        resizedBitmap.recycle();

        if (rotatedBitmap != null) {
            rotatedBitmap.recycle();
        }

    } catch (IOException e) {
        Utils.log("Image", e.getMessage() + e);
    }
}


private static Bitmap fetchRoughBitmap(String pathOfInputImage, float factor)
{
    Bitmap roughBitmap = null;
    try
    {
        FileInputStream in = new FileInputStream(pathOfInputImage);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = Math.round(factor);
        roughBitmap = BitmapFactory.decodeStream(in, null, options);
        return roughBitmap;
    }
    catch (OutOfMemoryError mem_error)
    {
        return Utils.fetchRoughBitmap(pathOfInputImage, factor+1);
    }
    catch (FileNotFoundException e)
    {
        //return Utils.fetchRoughBitmap(pathOfInputImage, factor+1);
        return roughBitmap;
    }
}

这code正常工作,因为我通过一个递归循环运行它,直到它不会耗尽内存,但后来我得到的图像仅为300×300像素,我需要它至少为640×480。我的问题是:

This code works fine because I run it through a recursive loop until it doesn't run out of memory but then my resulting image is only about 300x300 px and I need it to be at least 640x480. My questions are:

  1. 谁能告诉我,为什么银河S3采用了高达90%的内存时,第一次加载的60%,我的应用程序,而不是HTC手机。
  2. 有什么错在重新大小code,它是让我的应用程序运行的内存正在做。

任何帮助将大大AP preciated我已经花了3天这个问题: - 。(

Any help will be greatly appreciated as I have already spent 3 days on this issue :-(.

推荐答案

我之类的解决了这个问题我自己。运行内存的应用程序没有做上述code什么。我加了垫子和我看到的位图对象,正在不断从previous意见活着。所以我删除视图中的图形,并解决了内存问题。

I sort of solved the issue myself. The app running out of memory didn't have to do anything with the above code. I added the MAT and I saw that the bitmap objects were being kept alive from previous views. So I removed the graphics from the view and it solved the memory issue.

这篇关于Android应用程序崩溃的三星Galaxy S3(内存不足错误)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-11 04:21