I  made a image gallery but  getting a memory error when I slide
photos.
Then I added bitmapfactory but I can run the program because It shows
error in the bitmapdrawable.
can anybody tell me how to solve this problem.

ContentResolver cr=getContentResolver();
        Cursor c=cr.query(
            MediaStore.Images.Media.INTERNAL_CONTENT_URI,
            null,null,null,null);
        c.moveToFirst();
        for (int i=0;i<c.getCount();i++) {
            String path=c.getString(c.getColumnIndexOrThrow("_data"));
            photoList.add(file2bmp(path,480,480));
            for (String column:c.getColumnNames()) {//デバッグ出力
                android.util.Log.e("",column+"="+
                    c.getString(c.getColumnIndexOrThrow(column)));
            }
            c.moveToNext();
        }


        Gallery gallery=new Gallery(this);
        gallery.setSpacing(2);
        gallery.setAdapter(new GalleryAdapter());
        gallery.setLayoutParams(new LinearLayout.LayoutParams(FP,FP));
        layout.addView(gallery);
    }


    private Bitmap file2bmp(String path,int maxW,int maxH) {
        BitmapFactory.Options options;
        InputStream in=null;
        try {

            options=new BitmapFactory.Options();
            options.inJustDecodeBounds=true;
            in=new FileInputStream(path);
            BitmapFactory.decodeStream(in,null,options);
            in.close();
            int scaleW=options.outWidth/maxW+1;
            int scaleH=options.outHeight/maxH+1;
            int scale =Math.max(scaleW,scaleH);


            options=new BitmapFactory.Options();
            options.inJustDecodeBounds=false;
            options.inSampleSize=scale;
            in=new FileInputStream(path);
            Bitmap bmp=BitmapFactory.decodeStream(in,null,options);
            in.close();
            return bmp;
        } catch (Exception e) {
            try {
                if (in!=null) in.close();
            } catch (Exception e2) {
            }
            return null;
        }
    }


    public class GalleryAdapter extends BaseAdapter {

        public int getCount() {
            return photoList.size();
        }


        public Object getItem(int position) {
            return position;
        }


        public long getItemId(int position) {
            return position;
        }


        public View getView(int position, View convertView, ViewGroup
parent) {
            ImageView imageView;
            if (convertView==null) {
                imageView=new ImageView(MainAct.this);
                imageView.setLayoutParams(new
Gallery.LayoutParams(240,240));
 
imageView.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                imageView.setBackgroundColor(Color.BLACK);

                BitmapDrawable bd=new
BitmapDrawable(photoList.get(position));   //getting error
                imageView.setImageDrawable(bd);
            } else {
                imageView=(ImageView)convertView;
            }
            return imageView;
        }
    }


Thank you

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to