Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
233 views
in Technique[技术] by (71.8m points)

android - Outofmemory error in bitmap runtime exception

i am displaying my images from assests/image folder , but this code is not working . this code display images from assets folder in gallery . i am using gallery prefine library or jar file.

please expert check it . thank u

AssetManager assetManager = getAssets();
    String[] files = null;
    try {
        files = assetManager.list("image");
    } catch (IOException e) {
        Log.e("tag", e.getMessage());
    }

    for(String filename : files) {
        System.out.println("File name => "+filename);
        InputStream in = null;
        try {
            ImageViewTouch imageView = new ImageViewTouch(Rahul.this); 
            imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
          final Options options = new Options();
            options.outHeight = (int) scaleHeight; 
            options.outWidth = (int) scaleWidth;   
            options.inScaled = true;
            options.inPurgeable = true;
            options.inSampleSize = 2;
           in = assetManager.open("image/"+filename); 
           Bitmap bit=BitmapFactory.decodeStream(in);

          imageView.setImageBitmap(bit);

              } catch(Exception e) {
            Log.e("tag", e.getMessage());
        }
    }
    gallery.setAdapter(arrayAdapter);
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Hey please check my answer on the same issue: bitmap size exceeds Vm budget error android

And also always try to use maximum options while dealing with bitmaps like this:

 final Options options = new Options();
    options.outHeight = (int) scaleHeight; // new smaller height
    options.outWidth = (int) scaleWidth;   // new smaller width
    options.inScaled = true;
    options.inPurgeable = true;

    // to scale the image to 1/8
    options.inSampleSize = 8;
    bitmap = BitmapFactory.decodeFile(imagePath, options);

This might solve your problem.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...