0

I am trying to make a photo processing app in Android. when I try to read the photo from src file, it returned OutOfMemoryError

I tried to use

Bitmap.createBitmap(src.width, src.height, src.getConfig());

and the input photo is just a normal picture with 29KB that's totally weird I also tried this

Strange out of memory issue while loading an image to a Bitmap object

but results are the same, could anyone tell me what's wrong with this??? I totally out of idea...

Some part of my code used

ImageView image, processedImage;
Bitmap bitmapImage;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    image = (ImageView)findViewById(R.id.testingImage);
    processedImage = (ImageView)findViewById(R.id.prcoessedImage);

    InputStream is = this.getResources().openRawResource(R.drawable.testingimage);

    bitmapImage = BitmapFactory.decodeStream(is);
    imageProcessing(bitmapImage);
}

public static Bitmap imageProcessing(Bitmap src) {
    int width = src.getWidth();
    int height = src.getHeight();
    Bitmap result = Bitmap.createBitmap(width, height, src.getConfig());
    ...
}

logcat

08-31 15:50:54.694: D/ddm-heap(5497): Got feature list request
08-31 15:50:54.842: D/dalvikvm(5497): Trying to load lib lib_glossary.so 0x0
08-31 15:50:54.842: I/dalvikvm(5497): Unable to dlopen(lib_glossary.so): Cannot load library: load_library[1051]: Library 'lib_glossary.so' not found
08-31 15:50:54.842: E/MBGlossaryManager(5497): lib_glossary.so can't be loaded
08-31 15:50:55.178: D/dalvikvm(5497): GC freed 699 objects / 54432 bytes in 300ms
08-31 15:50:56.671: D/dalvikvm(5497): GC freed 65 objects / 2600 bytes in 230ms
08-31 15:50:59.374: D/dalvikvm(5497): GC freed 178 objects / 7992 bytes in 36ms
08-31 15:50:59.374: E/dalvikvm-heap(5497): 6432832-byte external allocation too large for this process.
08-31 15:50:59.374: E/(5497): VM won't let us allocate 6432832 bytes
08-31 15:50:59.374: D/AndroidRuntime(5497): Shutting down VM
08-31 15:50:59.374: W/dalvikvm(5497): threadid=3: thread exiting with uncaught exception (group=0x4001e2e0)
08-31 15:50:59.374: E/AndroidRuntime(5497): Uncaught handler: thread main exiting due to uncaught exception
08-31 15:50:59.374: E/AndroidRuntime(5497): java.lang.OutOfMemoryError: bitmap size exceeds VM budget
08-31 15:50:59.374: E/AndroidRuntime(5497):     at android.graphics.Bitmap.nativeCreate(Native Method)
08-31 15:50:59.374: E/AndroidRuntime(5497):     at android.graphics.Bitmap.createBitmap(Bitmap.java:494)
08-31 15:50:59.374: E/AndroidRuntime(5497):     at android.graphics.Bitmap.createBitmap(Bitmap.java:461)
08-31 15:50:59.374: E/AndroidRuntime(5497):     at android.graphics.Bitmap.createScaledBitmap(Bitmap.java:366)
08-31 15:50:59.374: E/AndroidRuntime(5497):     at com.image.process.MainActivity.onCreate(MainActivity.java:42)
08-31 15:50:59.374: E/AndroidRuntime(5497):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-31 15:50:59.374: E/AndroidRuntime(5497):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2521)
08-31 15:50:59.374: E/AndroidRuntime(5497):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2574)
08-31 15:50:59.374: E/AndroidRuntime(5497):     at android.app.ActivityThread.access$2400(ActivityThread.java:121)
08-31 15:50:59.374: E/AndroidRuntime(5497):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1925)
08-31 15:50:59.374: E/AndroidRuntime(5497):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-31 15:50:59.374: E/AndroidRuntime(5497):     at android.os.Looper.loop(Looper.java:136)
08-31 15:50:59.374: E/AndroidRuntime(5497):     at android.app.ActivityThread.main(ActivityThread.java:4425)
08-31 15:50:59.374: E/AndroidRuntime(5497):     at java.lang.reflect.Method.invokeNative(Native Method)
08-31 15:50:59.374: E/AndroidRuntime(5497):     at java.lang.reflect.Method.invoke(Method.java:521)
08-31 15:50:59.374: E/AndroidRuntime(5497):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:850)
08-31 15:50:59.374: E/AndroidRuntime(5497):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
08-31 15:50:59.374: E/AndroidRuntime(5497):     at dalvik.system.NativeStart.main(Native Method)
08-31 15:50:59.397: I/dalvikvm(5497): threadid=7: reacting to signal 3
08-31 15:50:59.397: I/dalvikvm(5497): Wrote stack trace to '/data/anr/traces.txt'
Community
  • 1
  • 1
ChristopherF
  • 125
  • 1
  • 2
  • 13
  • post your image processing code. – moDev Aug 31 '12 at 06:30
  • what are src.width and src.height of the image? (asking because even 29kb file could has very big dimensions and so, would need a lot of memory). – sandrstar Aug 31 '12 at 06:33
  • the src is the input of the image I use this to load the image InputStream is = this.getResources().openRawResource(R.drawable.testingimage); Bitmap src = BitmapFactory.decodeStream(is); – ChristopherF Aug 31 '12 at 06:39
  • before passing bitmap to src , try to recycle bitmap – moDev Aug 31 '12 at 06:43
  • how to measure the dimensions of the image?? – ChristopherF Aug 31 '12 at 06:44
  • ImageView.getDrawable().getIntrinsicHeight() ImageView.getDrawable().getIntrinsicWidth() – moDev Aug 31 '12 at 06:45
  • Agrawal your recycle bitmap means something like bitmap.recycle()?? because I just initial the bitmap when pass to src, so I guess there're not way to recycle, right?? please teach :) – ChristopherF Aug 31 '12 at 06:49
  • yes you are initializing for the first time, but if you are using many images using src then its good to recycle before passing src to bitmap.Need to see your code, i dont think just 29kb image can cause memory issue – moDev Aug 31 '12 at 06:52
  • I post some of my code above, please check, thank you very much :) – ChristopherF Aug 31 '12 at 06:59
  • bitmapImage = BitmapFactory.decodeStream(is); Instead of this line try this line bitmapImage = BitmapFactory.decodeStream(new InputStream(is)); Why you are using static bitmap method, that might be causing the issue – moDev Aug 31 '12 at 07:03
  • I got this "Cannot instantiate the type InputStream" = =" – ChristopherF Aug 31 '12 at 07:06
  • I am so stupid... I don't quite get what you mean... change the code with this bitmapImage = BitmapFactory.decodeStream(new InputStream(is)); right??? or what others??? I am I am not smart on this... – ChristopherF Aug 31 '12 at 07:12
  • yes , i have posted my answer will solve your issue – moDev Aug 31 '12 at 07:13
  • sorry not solved by the way I use Android 2.1 as developing environment... – ChristopherF Aug 31 '12 at 07:19
  • how you are setting bitmap on image?? please try without static method.That's the issue – moDev Aug 31 '12 at 07:26
  • what do you mean by bitmap setting, I just use the code above nothing more... and removing static method doesn't help... – ChristopherF Aug 31 '12 at 07:33
  • just remove your static method and calling method line following my answer , it will help – moDev Aug 31 '12 at 07:37
  • tried but problem still exist is it related to my testing image or some other problem?? – ChristopherF Aug 31 '12 at 07:43
  • instead of processedImage try testing image. post logcat for errors – moDev Aug 31 '12 at 07:46
  • post your 42nd line code from MainActivity – moDev Aug 31 '12 at 08:00
  • sorry left a while.. 42nd line code is the one you ask me to "Bitmap bitmap = Bitmap.createScaledBitmap(bitmapImage , bitmapImage.getWidth(), bitmapImage.getHeight(), false);" I changed a to false instead of null as it didn't allow me to use null, and I tried true, but results are the same... – ChristopherF Aug 31 '12 at 15:52

1 Answers1

2
Bitmap bitmap = Bitmap.createScaledBitmap(bitmapImage , bitmapImage.getWidth(),  bitmapImage.getHeight(), null);

processedImage.setImageBitmap(bitmap);
moDev
  • 5,248
  • 4
  • 33
  • 63