Decode the file to Bitmap in Android throwing exception [duplicate]

Web Technologies Web Development 2 years ago

0 1 0 0 0 tuteeHUB earn credit +10 pts

5 Star Rating 1 Rating
_x000D_ _x000D_ Possible Duplicate: Android: Strange out of memory issue while loading an image to a Bitmap object I developed the application to decode a file to Bitmap. I done the following code but it always throwing out of memory exception private Bitmap decodeFile(File f) { Bitmap bmp = null; FileInputStream fis = null; try { // decode image size BitmapFactory.Options o = new BitmapFactory.Options(); o.inTempStorage = new byte[16*1024]; o.inJustDecodeBounds = true; o.inPurgeable=true; //if(!f.exists()) f.createNewFile(); if(!f.exists()) f.createNewFile(); fis = new FileInputStream(f); BitmapFactory.decodeStream(fis, null, o); try { fis.close(); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } fis = null; // Find the correct scale value. It should be the power of 2. final int REQUIRED_SIZE = 70; int width_tmp = o.outWidth, height_tmp = o.outHeight; int scale = 1; while (true) { if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) break; width_tmp /= 2; height_tmp /= 2; scale *= 2; } // decode with inSampleSize BitmapFactory.Options o2 = new BitmapFactory.Options(); o2.inTempStorage = new byte[16*1024]; o2.inSampleSize = scale; o2.inPurgeable=true; o2.inTempStorage = new byte[16*1024]; fis = new FileInputStream(f); bmp = BitmapFactory.decodeStream(fis); if (fis != null) fis.close(); fis = null; return bmp; // return BitmapFactory.decodeFile(f.getAbsolutePath(), o2); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } finally { System.gc(); try { if (fis != null) fis.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } fis = null; } return null; } Added the log cat 12-03 12:03:05.070: E/AndroidRuntime(30696): FATAL EXCEPTION: main 12-03 12:03:05.070: E/AndroidRuntime(30696): java.lang.OutOfMemoryError 12-03 12:03:05.070: E/AndroidRuntime(30696): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method) 12-03 12:03:05.070: E/AndroidRuntime(30696): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:527) 12-03 12:03:05.070: E/AndroidRuntime(30696): at android.graphics.BitmapFactory.decodeFile(BitmapFactory.java:301) 12-03 12:03:05.070: E/AndroidRuntime(30696): at com.dexterity.iPinion.RootActivity.readFromSDCard(RootActivity.java:1984) 12-03 12:03:05.070: E/AndroidRuntime(30696): at com.dexterity.iPinion.RootActivity.setBrandingImage(RootActivity.java:285) 12-03 12:03:05.070: E/AndroidRuntime(30696): at com.dexterity.iPinion.RootActivity.access$0(RootActivity.java:252) 12-03 12:03:05.070: E/AndroidRuntime(30696): at com.dexterity.iPinion.RootActivity$1.handleMessage(RootActivity.java:515) 12-03 12:03:05.070: E/AndroidRuntime(30696): at android.os.Handler.dispatchMessage(Handler.java:99) 12-03 12:03:05.070: E/AndroidRuntime(30696): at android.os.Looper.loop(Looper.java:137) 12-03 12:03:05.070: E/AndroidRuntime(30696): at android.app.ActivityThread.main(ActivityThread.java:4745) 12-03 12:03:05.070: E/AndroidRuntime(30696): at java.lang.reflect.Method.invokeNative(Native Method) 12-03 12:03:05.070: E/AndroidRuntime(30696): at java.lang.reflect.Method.invoke(Method.java:511) 12-03 12:03:05.070: E/AndroidRuntime(30696): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 12-03 12:03:05.070: E/AndroidRuntime(30696): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 12-03 12:03:05.070: E/AndroidRuntime(30696): at dalvik.system.NativeStart.main(Native Method)

Posted on 16 Aug 2022, this text provides information on Web Development related to Web Technologies. Please note that while accuracy is prioritized, the data presented might not be entirely correct or up-to-date. This information is offered for general knowledge and informational purposes only, and should not be considered as a substitute for professional advice.

Take Quiz To Earn Credits!

Turn Your Knowledge into Earnings.

tuteehub_quiz

Answers (1)

Post Answer
profilepic.png
manpreet Tuteehub forum best answer Best Answer 2 years ago
_x000D_ You are setting BitmapFactory.Options but you are not passing them to your BitmapFactory.decodeStream. You have: BitmapFactory.decodeStream(fis); Should be: BitmapFactory.decodeStream(fis, o2); Also read Android's displaying bitmaps Also, depending on your device you may have very little runtime memory. This is especially true with older devices being 24MB or less, most newer devices are 64MB or more. You can check with Runtime.getRuntime().maxMemory() In addition you can force a large heap by adding 'android:largeHeap="true" ' to your AndroidManifest.xml file. on my Galaxy Nexus that pushes my heap to 254MB.

No matter what stage you're at in your education or career, TuteeHub will help you reach the next level that you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice sessions to improve your knowledge and scores.

Important Web Technologies Links