Hi, you can decode the image bounds and calculate the sample size.code as below:
1.get the image infos: BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; Bitmap bitmap = BitmapFactory.decodeFile(filePath, options); picWidth = options.outWidth; picHeight = options.outHeight; picAspect = (float) picHeight / (float) picWidth; 2.calculate the sample size: int be = (int) (width / screenWidth); if (be <= 0) be = 1; BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = be; 3.create the Bitmap: try { mBitmap = BitmapFactory.decodeStream( mContentResolver.openInputStream(mUri), null, options); } return mBitmap; } catch (OutOfMemoryError e) { System.gc(); e.printStackTrace(); if (mBitmap != null && mBitmap.isRecycled()) { mBitmap.recycle(); mBitmap = null; } } Best regards, ted.xie 在 2012年7月16日星期一UTC+8下午8时23分50秒,B.Arunkumar写道: > > Hi, > > We have an app which renders bitmaps on imageview. On Samsung > Galaxy, it works fine. But On Motorola Droid, it runs into memory > issues and crashes every now and then with the following logcat. > > /dalvikvm-heap( 3640): 3686400-byte external allocation too large for > this process. > E/GraphicsJNI( 3640): VM won't let us allocate 3686400 bytes > W/dalvikvm( 3640): threadid=11: thread exiting with uncaught exception > (group=0x4001d7e0) > E/AndroidRuntime( 3640): FATAL EXCEPTION: Thread-25 > E/AndroidRuntime( 3640): java.lang.OutOfMemoryError: bitmap size > exceeds VM budget > E/AndroidRuntime( 3640): at > android.graphics.Bitmap.nativeCreate(Native Method) > E/AndroidRuntime( 3640): at > android.graphics.Bitmap.createBitmap(Bitmap.java:468) > E/AndroidRuntime( 3640): at > com.example.OnVRViewer.RTPClient.ProcessFrame(RTPClient.java:419) > E/AndroidRuntime( 3640): at > com.example.OnVRViewer.RTPClient.ProcessRtpVideoPacket(RTPClient.java: > 204) > E/AndroidRuntime( 3640): at > com.example.OnVRViewer.RTSPUDPclient.AdvanceProcess(RTSPUDPclient.java: > 659) > E/AndroidRuntime( 3640): at > com.example.OnVRViewer.RTSPUDPclient.processSelectionKey(RTSPUDPclient.java: > > 726) > E/AndroidRuntime( 3640): at > com.example.OnVRViewer.RTSPUDPclient.run(RTSPUDPclient.java:2068) > E/AndroidRuntime( 3640): at java.lang.Thread.run(Thread.java:1096) > W/ActivityManager( 1081): Force finishing activity > com.example.OnVRViewer/.AsyncRecordTrial > > My question is even though we have a try catch on Bitmap.createBitmap > it is still crashing. Why is the catch block not working? And how do > we handle the exception without crashing on Motorola Droid. > > Following is my code right now: > > try > { > bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); > } > catch (Exception ex) > { > android.util.Log.e("Bitmap", "Error"); > } > > Thank you, > B.Arunkumar 在 2012年7月16日星期一UTC+8下午8时23分50秒,B.Arunkumar写道: > > Hi, > > We have an app which renders bitmaps on imageview. On Samsung > Galaxy, it works fine. But On Motorola Droid, it runs into memory > issues and crashes every now and then with the following logcat. > > /dalvikvm-heap( 3640): 3686400-byte external allocation too large for > this process. > E/GraphicsJNI( 3640): VM won't let us allocate 3686400 bytes > W/dalvikvm( 3640): threadid=11: thread exiting with uncaught exception > (group=0x4001d7e0) > E/AndroidRuntime( 3640): FATAL EXCEPTION: Thread-25 > E/AndroidRuntime( 3640): java.lang.OutOfMemoryError: bitmap size > exceeds VM budget > E/AndroidRuntime( 3640): at > android.graphics.Bitmap.nativeCreate(Native Method) > E/AndroidRuntime( 3640): at > android.graphics.Bitmap.createBitmap(Bitmap.java:468) > E/AndroidRuntime( 3640): at > com.example.OnVRViewer.RTPClient.ProcessFrame(RTPClient.java:419) > E/AndroidRuntime( 3640): at > com.example.OnVRViewer.RTPClient.ProcessRtpVideoPacket(RTPClient.java: > 204) > E/AndroidRuntime( 3640): at > com.example.OnVRViewer.RTSPUDPclient.AdvanceProcess(RTSPUDPclient.java: > 659) > E/AndroidRuntime( 3640): at > com.example.OnVRViewer.RTSPUDPclient.processSelectionKey(RTSPUDPclient.java: > > 726) > E/AndroidRuntime( 3640): at > com.example.OnVRViewer.RTSPUDPclient.run(RTSPUDPclient.java:2068) > E/AndroidRuntime( 3640): at java.lang.Thread.run(Thread.java:1096) > W/ActivityManager( 1081): Force finishing activity > com.example.OnVRViewer/.AsyncRecordTrial > > My question is even though we have a try catch on Bitmap.createBitmap > it is still crashing. Why is the catch block not working? And how do > we handle the exception without crashing on Motorola Droid. > > Following is my code right now: > > try > { > bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); > } > catch (Exception ex) > { > android.util.Log.e("Bitmap", "Error"); > } > > Thank you, > B.Arunkumar 在 2012年7月16日星期一UTC+8下午8时23分50秒,B.Arunkumar写道: > > Hi, > > We have an app which renders bitmaps on imageview. On Samsung > Galaxy, it works fine. But On Motorola Droid, it runs into memory > issues and crashes every now and then with the following logcat. > > /dalvikvm-heap( 3640): 3686400-byte external allocation too large for > this process. > E/GraphicsJNI( 3640): VM won't let us allocate 3686400 bytes > W/dalvikvm( 3640): threadid=11: thread exiting with uncaught exception > (group=0x4001d7e0) > E/AndroidRuntime( 3640): FATAL EXCEPTION: Thread-25 > E/AndroidRuntime( 3640): java.lang.OutOfMemoryError: bitmap size > exceeds VM budget > E/AndroidRuntime( 3640): at > android.graphics.Bitmap.nativeCreate(Native Method) > E/AndroidRuntime( 3640): at > android.graphics.Bitmap.createBitmap(Bitmap.java:468) > E/AndroidRuntime( 3640): at > com.example.OnVRViewer.RTPClient.ProcessFrame(RTPClient.java:419) > E/AndroidRuntime( 3640): at > com.example.OnVRViewer.RTPClient.ProcessRtpVideoPacket(RTPClient.java: > 204) > E/AndroidRuntime( 3640): at > com.example.OnVRViewer.RTSPUDPclient.AdvanceProcess(RTSPUDPclient.java: > 659) > E/AndroidRuntime( 3640): at > com.example.OnVRViewer.RTSPUDPclient.processSelectionKey(RTSPUDPclient.java: > > 726) > E/AndroidRuntime( 3640): at > com.example.OnVRViewer.RTSPUDPclient.run(RTSPUDPclient.java:2068) > E/AndroidRuntime( 3640): at java.lang.Thread.run(Thread.java:1096) > W/ActivityManager( 1081): Force finishing activity > com.example.OnVRViewer/.AsyncRecordTrial > > My question is even though we have a try catch on Bitmap.createBitmap > it is still crashing. Why is the catch block not working? And how do > we handle the exception without crashing on Motorola Droid. > > Following is my code right now: > > try > { > bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); > } > catch (Exception ex) > { > android.util.Log.e("Bitmap", "Error"); > } > > Thank you, > B.Arunkumar 在 2012年7月16日星期一UTC+8下午8时23分50秒,B.Arunkumar写道: > > Hi, > > We have an app which renders bitmaps on imageview. On Samsung > Galaxy, it works fine. But On Motorola Droid, it runs into memory > issues and crashes every now and then with the following logcat. > > /dalvikvm-heap( 3640): 3686400-byte external allocation too large for > this process. > E/GraphicsJNI( 3640): VM won't let us allocate 3686400 bytes > W/dalvikvm( 3640): threadid=11: thread exiting with uncaught exception > (group=0x4001d7e0) > E/AndroidRuntime( 3640): FATAL EXCEPTION: Thread-25 > E/AndroidRuntime( 3640): java.lang.OutOfMemoryError: bitmap size > exceeds VM budget > E/AndroidRuntime( 3640): at > android.graphics.Bitmap.nativeCreate(Native Method) > E/AndroidRuntime( 3640): at > android.graphics.Bitmap.createBitmap(Bitmap.java:468) > E/AndroidRuntime( 3640): at > com.example.OnVRViewer.RTPClient.ProcessFrame(RTPClient.java:419) > E/AndroidRuntime( 3640): at > com.example.OnVRViewer.RTPClient.ProcessRtpVideoPacket(RTPClient.java: > 204) > E/AndroidRuntime( 3640): at > com.example.OnVRViewer.RTSPUDPclient.AdvanceProcess(RTSPUDPclient.java: > 659) > E/AndroidRuntime( 3640): at > com.example.OnVRViewer.RTSPUDPclient.processSelectionKey(RTSPUDPclient.java: > > 726) > E/AndroidRuntime( 3640): at > com.example.OnVRViewer.RTSPUDPclient.run(RTSPUDPclient.java:2068) > E/AndroidRuntime( 3640): at java.lang.Thread.run(Thread.java:1096) > W/ActivityManager( 1081): Force finishing activity > com.example.OnVRViewer/.AsyncRecordTrial > > My question is even though we have a try catch on Bitmap.createBitmap > it is still crashing. Why is the catch block not working? And how do > we handle the exception without crashing on Motorola Droid. > > Following is my code right now: > > try > { > bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); > } > catch (Exception ex) > { > android.util.Log.e("Bitmap", "Error"); > } > > Thank you, > B.Arunkumar 在 2012年7月16日星期一UTC+8下午8时23分50秒,B.Arunkumar写道: > > Hi, > > We have an app which renders bitmaps on imageview. On Samsung > Galaxy, it works fine. But On Motorola Droid, it runs into memory > issues and crashes every now and then with the following logcat. > > /dalvikvm-heap( 3640): 3686400-byte external allocation too large for > this process. > E/GraphicsJNI( 3640): VM won't let us allocate 3686400 bytes > W/dalvikvm( 3640): threadid=11: thread exiting with uncaught exception > (group=0x4001d7e0) > E/AndroidRuntime( 3640): FATAL EXCEPTION: Thread-25 > E/AndroidRuntime( 3640): java.lang.OutOfMemoryError: bitmap size > exceeds VM budget > E/AndroidRuntime( 3640): at > android.graphics.Bitmap.nativeCreate(Native Method) > E/AndroidRuntime( 3640): at > android.graphics.Bitmap.createBitmap(Bitmap.java:468) > E/AndroidRuntime( 3640): at > com.example.OnVRViewer.RTPClient.ProcessFrame(RTPClient.java:419) > E/AndroidRuntime( 3640): at > com.example.OnVRViewer.RTPClient.ProcessRtpVideoPacket(RTPClient.java: > 204) > E/AndroidRuntime( 3640): at > com.example.OnVRViewer.RTSPUDPclient.AdvanceProcess(RTSPUDPclient.java: > 659) > E/AndroidRuntime( 3640): at > com.example.OnVRViewer.RTSPUDPclient.processSelectionKey(RTSPUDPclient.java: > > 726) > E/AndroidRuntime( 3640): at > com.example.OnVRViewer.RTSPUDPclient.run(RTSPUDPclient.java:2068) > E/AndroidRuntime( 3640): at java.lang.Thread.run(Thread.java:1096) > W/ActivityManager( 1081): Force finishing activity > com.example.OnVRViewer/.AsyncRecordTrial > > My question is even though we have a try catch on Bitmap.createBitmap > it is still crashing. Why is the catch block not working? And how do > we handle the exception without crashing on Motorola Droid. > > Following is my code right now: > > try > { > bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); > } > catch (Exception ex) > { > android.util.Log.e("Bitmap", "Error"); > } > > Thank you, > B.Arunkumar 在 2012年7月16日星期一UTC+8下午8时23分50秒,B.Arunkumar写道: > > Hi, > > We have an app which renders bitmaps on imageview. On Samsung > Galaxy, it works fine. But On Motorola Droid, it runs into memory > issues and crashes every now and then with the following logcat. > > /dalvikvm-heap( 3640): 3686400-byte external allocation too large for > this process. > E/GraphicsJNI( 3640): VM won't let us allocate 3686400 bytes > W/dalvikvm( 3640): threadid=11: thread exiting with uncaught exception > (group=0x4001d7e0) > E/AndroidRuntime( 3640): FATAL EXCEPTION: Thread-25 > E/AndroidRuntime( 3640): java.lang.OutOfMemoryError: bitmap size > exceeds VM budget > E/AndroidRuntime( 3640): at > android.graphics.Bitmap.nativeCreate(Native Method) > E/AndroidRuntime( 3640): at > android.graphics.Bitmap.createBitmap(Bitmap.java:468) > E/AndroidRuntime( 3640): at > com.example.OnVRViewer.RTPClient.ProcessFrame(RTPClient.java:419) > E/AndroidRuntime( 3640): at > com.example.OnVRViewer.RTPClient.ProcessRtpVideoPacket(RTPClient.java: > 204) > E/AndroidRuntime( 3640): at > com.example.OnVRViewer.RTSPUDPclient.AdvanceProcess(RTSPUDPclient.java: > 659) > E/AndroidRuntime( 3640): at > com.example.OnVRViewer.RTSPUDPclient.processSelectionKey(RTSPUDPclient.java: > > 726) > E/AndroidRuntime( 3640): at > com.example.OnVRViewer.RTSPUDPclient.run(RTSPUDPclient.java:2068) > E/AndroidRuntime( 3640): at java.lang.Thread.run(Thread.java:1096) > W/ActivityManager( 1081): Force finishing activity > com.example.OnVRViewer/.AsyncRecordTrial > > My question is even though we have a try catch on Bitmap.createBitmap > it is still crashing. Why is the catch block not working? And how do > we handle the exception without crashing on Motorola Droid. > > Following is my code right now: > > try > { > bm = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); > } > catch (Exception ex) > { > android.util.Log.e("Bitmap", "Error"); > } > > Thank you, > B.Arunkumar -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en