-1

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);
Rahul Rawat
  • 999
  • 2
  • 17
  • 40

2 Answers2

1

1) try to use bitmap.recycle(); to release memory before setting a new bitmap to your images

BitmapDrawable drawable = (BitmapDrawable) myImage.getDrawable();
Bitmap bitmap = drawable.getBitmap();
if (bitmap != null)
{
    bitmap.recycle();
}

2) if your images are too large scale down them:

public static Bitmap decodeFile(File file, int requiredSize) {
        try {

            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(file), null, o);

            // The new size we want to scale to

            // Find the correct scale value. It should be the power of 2.
            int width_tmp = o.outWidth, height_tmp = o.outHeight;
            int scale = 1;
            while (true) {
                if (width_tmp / 2 < requiredSize
                        || height_tmp / 2 < requiredSize)
                    break;
                width_tmp /= 2;
                height_tmp /= 2;
                scale *= 2;
            }

            // Decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;

            Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(file),
                    null, o2);

            return bmp;

        } catch (FileNotFoundException e) {
        } finally {
        }
        return null;
    }

Update

something like this:

for(int i=0; i<it.size();i++) { 
    ImageViewTouch imageView = new ImageViewTouch(GalleryTouchTestActivity.this); 
    imageView.setLayoutParams(new Gallery.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); 
    Options options = new Options(); 
    options.inSampleSize = 2;
    String photoURL = it.get(i);

    BitmapDrawable drawable = (BitmapDrawable) imageView.getDrawable();
    Bitmap bitmap = drawable.getBitmap();
    if (bitmap != null)
    {
       bitmap.recycle();
    }

    bitmap = BitmapFactory.decodeFile(photoURL);

    imageView.setImageBitmap(bitmap); 
    arrayAdapter.add(imageView);
}
Bob
  • 22,810
  • 38
  • 143
  • 225
  • thank you for your reply , i am new in android . here , where i need to put this code. my actually code is :for(int i=0; i – Rahul Rawat Oct 30 '12 at 15:02
  • sir , i give me runtime excepton (suspended ) and ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1956 please check it sir – Rahul Rawat Oct 31 '12 at 06:44
  • please help me sir i am depend on your kindly answer. i changed my code by http://stackoverflow.com/questions/13151970/image-content-can-not-read-during-zoom/13152098#comment17892182_13152098 and only problem is that when i zoom image , i am unable to read clear content of image . thanku sir – Rahul Rawat Oct 31 '12 at 07:00
  • This approach, scales down your image. So the quality of result bitmap is lower than the quality of original image. Check if `bitmap.recycle();` without scaling the bitmap down solves your problem or not. Forget scaling and only use `bitmap.recycle();` and tell us the result. – Bob Oct 31 '12 at 07:07
  • i am sorry i am not understanding . i am new in android. where i need to check it . may you give me any code thank you sir – Rahul Rawat Oct 31 '12 at 07:12
  • Hi sir, again same exception throw – Rahul Rawat Oct 31 '12 at 07:21
  • tell me about exception details – Bob Oct 31 '12 at 07:31
  • This exception is thrown suspendexception ActivityThread.performLaunchActivity(ActivityThread$ActivityClientRecord, Intent) line: 1956 – Rahul Rawat Oct 31 '12 at 07:42
  • Hi sir, may i send you my code file if you have a few time for me. i have covered with this problem and unable to solved this problem . thank u – Rahul Rawat Oct 31 '12 at 07:50
  • hi expert , i have update my code please have a look and help me thank you – Rahul Rawat Oct 31 '12 at 08:15
  • I do not know anything about `suspendexception` – Bob Oct 31 '12 at 08:41
  • hi sir , i know you have a huge knowledge . may i give you my project . please help me . i have covered with serious problem thank you – Rahul Rawat Oct 31 '12 at 09:05
1

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.

Community
  • 1
  • 1
Shrikant Ballal
  • 7,067
  • 7
  • 41
  • 61
  • thank you . but a another problem is made here . when i zoom image , i can not read image content . – Rahul Rawat Oct 31 '12 at 05:35
  • Hi sir, this project run good but when i zoom image i can not read content of image . content not display clear . please sir help me – Rahul Rawat Oct 31 '12 at 09:49
  • thank you sir and please check my new answer at http://stackoverflow.com/questions/13151970/image-content-can-not-read-during-zoom i am waiting for your kindly response , i know that you will give second success answers. thank you – Rahul Rawat Oct 31 '12 at 11:54