我是Android开发的新手。
我的问题是我能够从sqlite
数据库中检索数据。
我正在将image
数据转换为image
。
我需要从数据库中检索所有图像数据并显示为图库。
有没有办法做到这一点。
以下是我的代码
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;
}
}
}
答案 0 :(得分:0)
这是一个示例,它会将blob数据转换为位图,你需要在你的服装中定义的imageview中设置它网格视图 :::
ByteArrayInputStream inputStream = new ByteArrayInputStream(cursor.getBlob(columnIndex));
Bitmap mBitmap = BitmapFactory.decodeStream(inputStream);
自定义网格视图::
1. Developer Example
2. Stealthcopter.com
3. Super android fighter Paresh mayani
4. For custom search...
答案 1 :(得分:0)
要从数据库中读取图像,请使用以下内容:
Cursor c = <your db object>.query(...);
while (c.moveToNext())
{
byte[] image = c.getBlob();
Bitmap bmp = BitmapFactory.decodeByteArray(image, 0, image.length);
imageView.setImageBitmap(bmp);
}