0

I am facing a strange problem of out of memory in my app. I have made R&D and i found many way to solve this problem but i have faced same issue. What exactly i m doing is captureing images from camera ----> storing it inside the sdcard -----> displaying eight(8) images in on screen like on row and two images (total 4 rows and 8 images) Here in this screen i can see bitmap images successfully in image view. But i have implemented one more functionality is when clicked on the any image view out of this 8 image view this image will be prompted as big images as what screen size is.

Below is my image code where i managing bitmap image from sdcard :

public Bitmap create_Bitmap(boolean cam, int type, boolean returnFull)
    {
        used = true;
        String dir="";
        dir = camDir;

         try
            {
                File Dir= Environment.getExternalStorageDirectory();
                File imageDirectory = new File(Dir,dir);
                File file  = new File(imageDirectory, "img_"+s_id+"_" +type+".jpg");
                decodeFile(file);

                if(file.exists()){

                    BitmapFactory.Options options = new BitmapFactory.Options();
                    options.inJustDecodeBounds = true;
                    options.inSampleSize = 8;

                    BitmapFactory.decodeFile(file.getAbsolutePath(),options);

                    int h = options.outHeight;
                    int w = options.outWidth;

                    if(returnFull)
                    {
                        if(bitmap!= null) bitmap.recycle();
                        options.inSampleSize = calculateInSampleSize(options, Full_width, Full_height);
                        options.inJustDecodeBounds = false;
                        bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());//,options);
                        if(h<w)
                            bitmap=rotateBitmap(bitmap);
                        return bitmap;
                    }

                    else
                    {
                        Bitmap bitmap1 = getThumbCache(cam, type);

                        if(bitmap1!=null)
                        {
                          return bitmap1;
                        }
                        else
                        {
                            options.inSampleSize = calculateInSampleSize(options, Thumb_width, Thumb_hieght);
                            options.inJustDecodeBounds = false;
                            options.inSampleSize = 8;
                            bitmap1 = BitmapFactory.decodeFile(file.getAbsolutePath(),options);

                            if(h<w)
                            {
                                bitmap1 = rotateBitmap(bitmap1);

                            }

                            StroreThumbCache(cam, type, bitmap1);
                            return bitmap1;
                        }
                    }
                }

            }
            catch (Exception e)
            {
                Log.i(tag, "Failed to load", e);
                System.gc();
            }

         return null;
    }

       public ManageBitmap(int s_id1, int th_h,int th_w, int full_h,int full_w)
    {
    if(used) { clean(); used = false; }

    s_id = s_id1;

    Thumb_hieght = th_h;
    Thumb_width =th_w;

    Full_height = full_h;
    Full_width = full_w;

    }       

    Bitmap rotateBitmap(Bitmap bitmap)
    {

        Matrix matrix = new Matrix();
        matrix.postRotate(90);
        Bitmap bitmap1 = Bitmap.createBitmap(bitmap, 0, 0, 
                bitmap.getWidth(), bitmap.getHeight(), 
                                      matrix, true);
        bitmap.recycle();
        bitmap=null;

        Log.i(tag, "------->Rotating<-------");
        return bitmap1;

    }

When time to clicking image out of those 8 images. I am getting below Logs :

06-07 15:09:57.903: D/AndroidRuntime(12941): Shutting down VM
06-07 15:09:57.903: W/dalvikvm(12941): threadid=1: thread exiting with uncaught exception (group=0x40ef5908)
06-07 15:09:57.911: E/AndroidRuntime(12941): FATAL EXCEPTION: main
06-07 15:09:57.911: E/AndroidRuntime(12941): java.lang.OutOfMemoryError
06-07 15:09:57.911: E/AndroidRuntime(12941):    at android.graphics.Bitmap.nativeCreate(Native Method)
06-07 15:09:57.911: E/AndroidRuntime(12941):    at android.graphics.Bitmap.createBitmap(Bitmap.java:640)
06-07 15:09:57.911: E/AndroidRuntime(12941):    at android.graphics.Bitmap.createBitmap(Bitmap.java:586)
06-07 15:09:57.911: E/AndroidRuntime(12941):    at com.ISCServices.iPhysioIntel.Photo.ManageBitmap.rotateBitmap(ManageBitmap.java:207)
06-07 15:09:57.911: E/AndroidRuntime(12941):    at com.ISCServices.iPhysioIntel.Photo.ManageBitmap.create_Bitmap(ManageBitmap.java:160)
06-07 15:09:57.911: E/AndroidRuntime(12941):    at com.ISCServices.iPhysioIntel.ViewAfterBeforeFullImage.onCreate(ViewAfterBeforeFullImage.java:33)
06-07 15:09:57.911: E/AndroidRuntime(12941):    at android.app.Activity.performCreate(Activity.java:5020)
06-07 15:09:57.911: E/AndroidRuntime(12941):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
06-07 15:09:57.911: E/AndroidRuntime(12941):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
06-07 15:09:57.911: E/AndroidRuntime(12941):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2211)
06-07 15:09:57.911: E/AndroidRuntime(12941):    at android.app.ActivityThread.access$600(ActivityThread.java:149)
06-07 15:09:57.911: E/AndroidRuntime(12941):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1300)
06-07 15:09:57.911: E/AndroidRuntime(12941):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-07 15:09:57.911: E/AndroidRuntime(12941):    at android.os.Looper.loop(Looper.java:153)
06-07 15:09:57.911: E/AndroidRuntime(12941):    at android.app.ActivityThread.main(ActivityThread.java:5086)
06-07 15:09:57.911: E/AndroidRuntime(12941):    at java.lang.reflect.Method.invokeNative(Native Method)
06-07 15:09:57.911: E/AndroidRuntime(12941):    at java.lang.reflect.Method.invoke(Method.java:511)
06-07 15:09:57.911: E/AndroidRuntime(12941):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
06-07 15:09:57.911: E/AndroidRuntime(12941):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
06-07 15:09:57.911: E/AndroidRuntime(12941):    at dalvik.system.NativeStart.main(Native Method)

and i m passing value as below somewhere in project before making imageview.

ManageBitmap((int)CommonData.CurrentSessionID , 90, 54, 200, 120);
4

1 回答 1

0

You have created second object of bitmap to return it back from "rotateBitmap". Isn't it possible to do following?

Bitmap rotateBitmap(Bitmap bitmap)
{
    Matrix matrix = new Matrix();
    matrix.postRotate(90);
    Log.i(tag, "------->Rotating<-------");
    return Bitmap.createBitmap(bitmap, 0, 0,
            bitmap.getWidth(), bitmap.getHeight(), matrix, true);
}

Edit

I like to suggest some points to notice on.

  • Make function for every method you are using.
  • whenever you want to use any "memory hunger" object, declare it with "WeakReference". After using it, it has method to clear reference. This will reduce memory when GC will get no reference to your Bitmap used in your method.
  • Don't load full sized bitmap, instead load bitmap with width and height specified.

Remove comment in below code,

bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());//,options);

to

bitmap = BitmapFactory.decodeFile(file.getAbsolutePath(),options);
于 2013-06-07T09:59:03.003 回答