简体   繁体   中英

Changing image property and saving back to sdCard in android

i am trying to develop a simple image editor , it will have basic functionality like rotating image, cropping , changing brightness and contrast.

As the images are very huge in size so i am creating a down scaled bitmap to fit into memory
( i am following this tutorial over developer docs :-
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html )

while saving i have to make the changes in original image, but i am getting out of memory exception while trying to do so :-

here is my code to change the brightness :-

public static Bitmap brightness(Bitmap original, int brightness) {
        Bitmap newBitmap = Bitmap.createBitmap(original.getWidth(),
                original.getHeight(), original.getConfig());
        int[] argb = new int[original.getWidth() * original.getHeight()];
        original.getPixels(argb, 0, original.getWidth(), 0, 0,
                original.getWidth(), original.getHeight());
        for (int i = argb.length - 1; i >= 0; --i) {
            int alpha = argb[i] >> 24;
            int red = (argb[i] >> 16) & 0xFF;
            int green = (argb[i] >> 8) & 0xFF;
            int blue = argb[i] & 0xFF;
            int red2 = red + brightness;
            if (red2 > 0xFF)
                red2 = 0xFF;
            if (red2 < 0)
                red2 = 0;
            int green2 = green + brightness;
            if (green2 > 0xFF)
                green2 = 0xFF;
            if (green2 < 0)
                green2 = 0;
            int blue2 = blue + brightness;
            if (blue2 > 0xFF)
                blue2 = 0xFF;
            if (blue2 < 0)
                blue2 = 0;
            int composite = (alpha << 24) | (red2 << 16) | (green2 << 8)| blue2;
            argb[i] = composite;
        }
        newBitmap.setPixels(argb, 0, original.getWidth(), 0, 0, original.getWidth(), original.getHeight());
        return newBitmap;
    }

and here is the log cat value :-

java.lang.RuntimeException: An error occured while executing doInBackground()
        at android.os.AsyncTask$3.done(AsyncTask.java:299)
        at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
        at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
        at java.util.concurrent.FutureTask.run(FutureTask.java:137)
        at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
        at java.lang.Thread.run(Thread.java:838)
 Caused by: java.lang.OutOfMemoryError
        at android.graphics.Bitmap.nativeCreate(Native Method)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:640)
        at android.graphics.Bitmap.createBitmap(Bitmap.java:620)
        at com.app.imageeditor.apputils.BitmapHelper.brightness(BitmapHelper.java:26)
        at com.app.imageeditor.activities.ImageActivity$BitmapEditor.doInBackground(ImageActivity.java:307)
        at com.app.imageeditor.activities.ImageActivity$BitmapEditor.doInBackground(ImageActivity.java:1)
        at android.os.AsyncTask$2.call(AsyncTask.java:287)
        at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
            at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
            at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
            at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
            at java.lang.Thread.run(Thread.java:838)

i am using async task to apply changes on original image by calling the method i specified above

help me getting out of this !!

Chances are you may want to be using something along the lines of BitmapRegionDecoder . BitmapRegionDecoder allows you to work with parts of an image. This is very handy if you are dealing with very large images.

You would also need to check if there are older Bitmaps hanging in the memory before you try to create new..

Make sure you call Bitmap.recycle() at every instance you create a new bitmap which is not usable now.

You can also use MAT tool provided by eclipse to analyse overall memory situation in your app.

There is no easy and straightforward solution to your problem. It is a very tricky part in android and needs some analysis.

Please find below some links which can help you : -

http://andmateclipse.blogspot.in/

http://android-developers.blogspot.in/2011/03/memory-analysis-for-android.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM