-1

I am new to android development. My question is i am able to retrieve the data from sqlite database. i am converting the image data in to image. I need to retrieve all the images data from database and display as image gallery. Is there any way to do it.

Below is my code

   import java.util.ArrayList;
   import android.app.Activity;
   import android.content.Context;
   import android.content.Intent;
   import android.content.res.TypedArray;
   import android.database.Cursor;
   import android.database.sqlite.SQLiteDatabase;
   import android.graphics.Bitmap;
   import android.graphics.BitmapFactory;
   import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;

public class task1images extends Activity  {
    private Integer[] myImageIds;
     private Gallery gallery;
     private ImageView imgView;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.images);     
        SQLiteDatabase myDB = this.openOrCreateDatabase("test.db", SQLiteDatabase.OPEN_READWRITE, null);
        try{
            Cursor c = myDB.rawQuery("select photo from messages where task=1 and photo!=''"  , null);  


            //get the gallery items from the cursor
            ArrayList galleryList = new ArrayList();
            for(c.moveToFirst();!c.isAfterLast();c.moveToNext()){
                Integer gal;
                gal = c.getInt(c.getColumnIndex("photo"));
                galleryList.add(gal);               
            }
            System.out.println("size:::"+galleryList.size());
            myImageIds =(Integer[])galleryList.toArray(new Integer[galleryList.size()]);
            System.out.println(myImageIds[0]);
            imgView = (ImageView)findViewById(R.id.ImageView01);
            imgView.setImageResource(myImageIds[0]);

            gallery = (Gallery) findViewById(R.id.examplegallery);
            gallery.setAdapter(new AddImgAdp(this));

            gallery.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView parent, View v, int position, long id) {
            imgView.setImageResource(myImageIds[position]);
            }
            });
          }
          catch(Exception e) {
           Log.e("Error", "Error", e);
          } finally {
           if (myDB != null)
            myDB.close();
          }
    }
    public class AddImgAdp extends BaseAdapter {
        int GalItemBg;
        private Context cont;

        public AddImgAdp(Context c) {
            cont = c;
            TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
            GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
            typArray.recycle();
        }

        public int getCount() {
            return myImageIds.length;
        }

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

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

        public View getView(int position, View convertView, ViewGroup parent) {
            ImageView imgView = new ImageView(cont);

            imgView.setImageResource(myImageIds[position]);
            imgView.setLayoutParams(new Gallery.LayoutParams(80, 70));
            imgView.setScaleType(ImageView.ScaleType.FIT_XY);
            imgView.setBackgroundResource(GalItemBg);

            return imgView;
        }
    }

}
Vamshi
  • 1,495
  • 1
  • 15
  • 31
  • You have to write a sql to fetch all the images in DB then as per resultset create a for Loop to create image from each tuple(row). – Roll no1 Dec 27 '11 at 08:19
  • Yes, i am able to loop the data but how can i add the data in to gallery. – Vamshi Dec 27 '11 at 08:23
  • Do *not* post the same question twice. If you have additional details to add to the original question, update *that* question. Posting the same question twice might result in further moderator action. – casperOne Dec 27 '11 at 14:30

3 Answers3

1

No need to do looping, you have make an adapter and set it to your gallery http://saigeethamn.blogspot.com/2010/05/gallery-view-android-developer-tutorial.html

EDITED:

in getView()

cursor.moveToPosition(position);
imageView.setImageBitmap(getImageFromBLOB(cursor.getBlob(cursor.getColumnIndex("fieldname"))));

Method: to get Bitmap from Blob

public Bitmap getImageFromBLOB(byte[] mBlob) {
    byte[] bb = mBlob;
    return BitmapFactory.decodeByteArray(bb, 0, bb.length);

}

Edited:

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View vi=convertView;
        ImageView mImageView;
        cursor.moveToPosition(position);

            if(convertView==null)
            {
                LayoutInflater inf=mActivity.getLayoutInflater();
                vi=inf.inflate(R.layout.xxxxxx, null);

                mImageView=(ImageView)vi.findViewById(R.id.imgview_row);
                 vi.setTag(mImageView);
            }
             else
                 mImageView=(ImageView)vi.getTag();

            try {

                cursor.moveToPosition(position); 
                mImageView.image.setImageBitmap(AllMethod.getImageFromBLOB(cursor.getBlob(cursor.getColumnIndex("fieldname"))));

            } catch (Exception e) {
                // TODO: handle exception
                e.printStackTrace();
            }
        }
        return vi;
    }
Mohammed Azharuddin Shaikh
  • 41,633
  • 14
  • 96
  • 115
  • Thanks for the reply but in this post images were in drawable folder, but i need to convert the data to image from database and display the images in gallary – Vamshi Dec 27 '11 at 08:49
  • @Vamshi: [this might also helpful for you](http://stackoverflow.com/a/3068012/593709) – Adil Soomro Dec 27 '11 at 08:51
  • are you storing image path or BLOB in DB? – Mohammed Azharuddin Shaikh Dec 27 '11 at 08:52
  • @hotveryspicy: hi am storing as blob – Vamshi Dec 27 '11 at 08:59
  • dnt add imgView.setBackgroundResource(GalItemBg); directly first see that your source image is coming or not. comment that line and try – Mohammed Azharuddin Shaikh Dec 27 '11 at 10:15
  • @hotveryspicy: Actually the data is coming as base64string – Vamshi Dec 27 '11 at 10:17
  • c.moveToFirst(); if (c != null) {int i = 0; do {i++; String photo = c.getString(Column1);if(photo!=""){ byte[] myByteArray = Base64EnCoder.decode(photo); Bitmap bmp=BitmapFactory.decodeByteArray(myByteArray,0,myByteArray.length); ImageView image = (ImageView)this.findViewById(R.id.GalleryView); image.setImageBitmap(bmp);byte[] myByteArray1 = Base64EnCoder.decode(photo); }}while(c.moveToNext());} it is displaying 1 image only – Vamshi Dec 27 '11 at 10:36
  • with above code i am able to display only 1 image in the screen, not gallery – Vamshi Dec 27 '11 at 10:38
  • your way is little complex, 'm having no time to understand. Better try and google it sorry – Mohammed Azharuddin Shaikh Dec 27 '11 at 10:44
0

Here is an Example, it will get blob data into bitmap and you need to set it in imageview which is define in your costume grid view :::

ByteArrayInputStream inputStream = new ByteArrayInputStream(cursor.getBlob(columnIndex));
Bitmap mBitmap = BitmapFactory.decodeStream(inputStream);

And Custom Gridview ::

1. Developer Example
2. Stealthcopter.com
3. Super android fighter Paresh mayani
4. For custom search...

Nikunj Patel
  • 21,853
  • 23
  • 89
  • 133
0

To read image from database use something like this:

Cursor c = <your db object>.query(...);
while (c.moveToNext())
{
 byte[] image = c.getBlob();
 Bitmap bmp = BitmapFactory.decodeByteArray(image, 0, image.length);
 imageView.setImageBitmap(bmp);
}
Alexander Mikhaylov
  • 1,790
  • 1
  • 13
  • 23