FATAL EXCEPTION: main java.lang.OutOfMemoryError at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)

后端 未结 4 1109
时光取名叫无心
时光取名叫无心 2021-01-15 04:42

I keep getting this error when I run my app. The app will compile fine and once I start interacting with it (ImageSlider) sometimes it breaks and comes up with that message.

相关标签:
4条回答
  • 2021-01-15 04:55

    you should use this......

          @Override
        public Object instantiateItem(ViewGroup container, int position) {
            final TouchImageView imgDisplay;
    
            inflater = (LayoutInflater) _activity
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View viewLayout = inflater.inflate(
                    R.layout.layout_fullscreen_image, container, false);
    
            imgDisplay = (TouchImageView) viewLayout
                    .findViewById(R.id.imgDisplay);
            final ProgressBar spinner = (ProgressBar) viewLayout
                    .findViewById(R.id.loading);
            // btnShare = (Button) viewLayout.findViewById(R.id.btnShare);
    
            imageLoader.displayImage(_imagePaths.get(position).get("url"),
                    imgDisplay, options, new ImageLoadingListener() {
                        @Override
                        public void onLoadingStarted() {
                            spinner.setVisibility(View.VISIBLE);
                        }
    
                        @Override
                        public void onLoadingFailed(FailReason failReason) {
                            String message = null;
                            switch (failReason) {
                            case IO_ERROR:
                                message = "Input/Output error";
                                break;
                            case OUT_OF_MEMORY:
                                message = "Out Of Memory error";
                                break;
                            case UNKNOWN:
                                message = "Unknown error";
                                break;
                            }
                            Toast.makeText(FullScreenViewActivity.this,
                                    message, Toast.LENGTH_SHORT).show();
    
                            spinner.setVisibility(View.GONE);
                            imgDisplay
                                    .setImageResource(android.R.drawable.ic_delete);
                        }
    
                        @Override
                        public void onLoadingComplete(Bitmap loadedImage) {
                            spinner.setVisibility(View.GONE);
                            Animation anim = AnimationUtils
                                    .loadAnimation(FullScreenViewActivity.this,
                                            R.anim.fade_in);
                            imgDisplay.setAnimation(anim);
                            anim.start();
    
                        }
    
                        @Override
                        public void onLoadingCancelled() {
                            // Do nothing
                        }
                    });
    
            ((ViewPager) container).addView(viewLayout, 0);
    
            return viewLayout;
        }
    
        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            ((ViewPager) container).removeView((FrameLayout) object);
    
        }
    }
    

    Add this code on onCreate() method..

          private DisplayImageOptions options;
    
          options = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.drawable.ic_launcher).cacheOnDisc()
                .imageScaleType(ImageScaleType.IN_SAMPLE_INT).build();
    

    Add universal-image-loader-1.6.1-with-src.jar jar file to your libs folder.

    UPDATE:

     private DisplayImageOptions options;
     public FullScreenImageAdapter(Activity activity,
            ArrayList<String> imagePaths) {
        this._activity = activity;
        this._imagePaths = imagePaths;
        options = new DisplayImageOptions.Builder()
                .showImageForEmptyUri(R.drawable.ic_launcher).cacheOnDisc()
                .imageScaleType(ImageScaleType.IN_SAMPLE_INT).build();
     }
    
    0 讨论(0)
  • 2021-01-15 04:59

    If Google docs didn't help (and they have some neat techniques), as a last resort you can try adding android:largeHeap="true" to the <application> tag in the AndroidManifest.xml. But really, last resort.

    0 讨论(0)
  • 2021-01-15 05:08

    You should look at this article on android dev site http://developer.android.com/training/displaying-bitmaps/index.html . You should load bitmap asynchronously, because you are decoding file and its expensive operation and you should also first load bitmap size and than scale it to real size of imageview

    0 讨论(0)
  • 2021-01-15 05:15

    if anyone trying to get images from sqlite and getting outOfMemory error try following :

    cursor = adapter.getAllrecords(); //get your table records in cursor
    
    //moving to the current position in cursor 
    cursor.moveToPosition(position);
    
    byte [] image = cursor.getBlob(cursor.getColumnIndex("image"));
    
     BitmapFactory.Options options = new  BitmapFactory.Options();
     options.inJustDecodeBounds = true;
     bitmapImage = BitmapFactory.decodeByteArray(image, 0, image.length, options);
     imageView.setImageBitmap(decodeSampledBitmapFromResource(80, 80));//pass height and             
                                                          //width as per your requirement 
    
    
    
    public  int calculateInSampleSize( BitmapFactory.Options options, int reqWidth, int     
    reqHeight) {
            // Raw height and width of image
            final int height = options.outHeight;
            final int width = options.outWidth;
            int inSampleSize = 1;
    
            if (height > reqHeight || width > reqWidth) {
    
                final int halfHeight = height / 2;
                final int halfWidth = width / 2;
    
    // Calculate the largest inSampleSize value that is a power of 2 and keeps both
    // height and width larger than the requested height and width.
                 while ((halfHeight / inSampleSize) > reqHeight
                        && (halfWidth / inSampleSize) > reqWidth) {
                    inSampleSize *= 2;
                }
            }
    
            return inSampleSize;
        }
            public  Bitmap decodeSampledBitmapFromResource(int reqWidth, int reqHeight) {
    
                // First decode with inJustDecodeBounds=true 
                    to check dimensions
                final BitmapFactory.Options options = new BitmapFactory.Options();
                options.inJustDecodeBounds = true;
                BitmapFactory.decodeByteArray(image, 0, image.length, options);
    
                // Calculate inSampleSize
                options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight);
    
                // Decode bitmap with inSampleSize set
                options.inJustDecodeBounds = false;
                return BitmapFactory.decodeByteArray(image, 0, image.length, options);
            }
    

    this code worked for me..however according to developer guideliness we should handle image related task using AsyncTask...we should not run it on UI thread.

    0 讨论(0)
提交回复
热议问题