android image view crashes when big image is chosen for display

和自甴很熟 提交于 2019-12-25 18:02:40

问题


Many have asked this question before and got the right way to resolve it. but somehow its not working out for me. My app crashes when I try to choose a large image for display. I have tried to reduce the size by scaling it as well using BitmapFactory. This is not working. Please help.

my code:

    public void browse(View v){
            @SuppressWarnings("unused")
            int id = v.getId();
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.addCategory(Intent.CATEGORY_OPENABLE);
            startActivityForResult(intent, REQUEST_CODE);
        }

        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            InputStream stream = null;

            if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK) {

                Uri imgUri = data.getData();
                logopath = getPath(imgUri);

                //This block is to show the view
                    try {
                        if(bitmap != null){
                            bitmap.recycle();
                        }
                            stream = getContentResolver().openInputStream(data.getData());
                            bitmap = BitmapFactory.decodeStream(stream);
                            iv.setImageBitmap(Bitmap.createScaledBitmap(bitmap, 100, 100, true));
                            //bitmap = shrinkBitmap(logopath,100,100);
                            //iv.setImageBitmap(bitmap);
                        } 
                    catch (FileNotFoundException e) {
                            e.printStackTrace();
                    }
                finally{
                    if (stream != null) {
                        try {
                                stream.close();
                            } 
                        catch (Exception e) {
                                e.printStackTrace();
                        }
                    }
                }

            }
        }

        private Bitmap shrinkBitmap(String file, int height, int width) {
            BitmapFactory.Options bmpFactoryOptions = new BitmapFactory.Options();
            bmpFactoryOptions.inJustDecodeBounds = true;
            Bitmap bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);

            int heightRatio = (int)Math.ceil(bmpFactoryOptions.outHeight/(float)height);
            int widthRatio = (int)Math.ceil(bmpFactoryOptions.outWidth/(float)width);

            if (heightRatio > 1 || widthRatio > 1)
            {
             if (heightRatio > widthRatio)
             {
              bmpFactoryOptions.inSampleSize = heightRatio;
             } else {
              bmpFactoryOptions.inSampleSize = widthRatio; 
             }
            }

            bmpFactoryOptions.inJustDecodeBounds = false;
            bitmap = BitmapFactory.decodeFile(file, bmpFactoryOptions);
         return bitmap;
        }

LogCat details:

    06-25 07:16:26.056: E/dalvikvm-heap(1973): Out of memory on a 48000016-byte allocation.
    06-25 07:16:26.206: E/AndroidRuntime(1973): FATAL EXCEPTION: main
    06-25 07:16:26.206: E/AndroidRuntime(1973): java.lang.OutOfMemoryError
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:529)
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:601)
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at com.example.tg_db1.edit_company.onActivityResult(edit_company.java:113)
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at android.app.Activity.dispatchActivityResult(Activity.java:5293)
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at android.app.ActivityThread.deliverResults(ActivityThread.java:3315)
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3362)
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at android.app.ActivityThread.access$1100(ActivityThread.java:141)
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1282)
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at android.os.Handler.dispatchMessage(Handler.java:99)
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at android.os.Looper.loop(Looper.java:137)
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at android.app.ActivityThread.main(ActivityThread.java:5039)
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at java.lang.reflect.Method.invokeNative(Native Method)
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at java.lang.reflect.Method.invoke(Method.java:511)
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
    06-25 07:16:26.206: E/AndroidRuntime(1973):     at dalvik.system.NativeStart.main(Native Method)

回答1:


Bitmap ThumbImage = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(imagePath), THUMBSIZE, THUMBSIZE);

Refer




回答2:


Use the sampling technique. More the sampling value, less the image size. Hope you will be able to handle the OutOfMemory error, which causes due to low heap size rendering large images sizes.

BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 15;



Bitmap bm = BitmapFactory.decodeStream(stream);

if you are using android 3.0 onward use android:largeHeap="true" tag within application tag in manifest file..

 application
        android:allowBackup="true"
        android:icon="@drawable/app_icon"
        android:label="@string/app_name"
        android:theme="@style/AppTheme>
        android:largeHeap="true"


来源:https://stackoverflow.com/questions/17291272/android-image-view-crashes-when-big-image-is-chosen-for-display

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!