我的数据库中有一些blob,实际上我只能检索一个并在ImageView中显示它,我想退休几个并给用户选择,就像他只是点击同一个按钮 ImageView的图像发生了变化 这是我的主要课程:
有我的主要类:
package com.example.autretest;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton imagebutton=(ImageButton)findViewById(R.id.imageButton1);
addListenerOnButton();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
public void addListenerOnButton() {
final ImageView imageview=(ImageView)findViewById(R.id.imageView1);
final Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
Bitmap bm = null;
List<Bitmap> listbitmap=null;
SQLiteAdapter mySQLiteAdapter =new SQLiteAdapter(getApplicationContext());
mySQLiteAdapter.openToRead();
listbitmap=mySQLiteAdapter.queueAllphoto();
int location=2;
while (location<listbitmap.size()){
imageview.setImageBitmap(listbitmap.get(location));
}
location++;
mySQLiteAdapter.close();
//Bitmap bmp = BitmapFactory.decodeByteArray(content,0,content.length);
//image = new BitmapDrawable(BitmapFactory.decodeByteArray(content, 0, content.length));
Toast.makeText(MainActivity.this,
"ImageButton is clicked!", Toast.LENGTH_SHORT).show();
}
});
}
}
然后你有了另一个类SqliteAdapter:
package com.example.autretest;
import java.sql.Blob;
import java.util.List;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class SQLiteAdapter {
public static final String MYDATABASE_NAME = "mydatabase";
public static final String MYDATABASE_TABLE_APP = "ma_table";
public static final String MYDATABASE_TABLE_photo = "pictures";
public static final String MYDATABASE_TABLE_plan = "plan";
public static final int MYDATABASE_VERSION = 1;
public static final String KEY_CONTENT = "Content";
public static final String KEY_CONTENT_ID = "Content_PK";
public static final String KEY_CONTENT_ID_photo = "Content_PK_photo";
public static final String KEY_CONTENT_ID_plan = "Content_PK_plan";
public static final String KEY_CONTENT_photo = "Content_photo";
public static final String KEY_CONTENT_plan = "Content_plan";
//create table MY_DATABASE (ID integer primary key, Content text not null);
private static final String SCRIPT_CREATE_DATABASE =
"create table " + MYDATABASE_TABLE_APP + " ("
+ KEY_CONTENT + " text not null)"+
"create table " + MYDATABASE_TABLE_photo + " ("+ KEY_CONTENT_photo + " blob not null)"+" " +
"("+ KEY_CONTENT_ID_photo + " INTEGER not null);" +" ("+ KEY_CONTENT_ID + " INTEGER not null) "+
"create table " + MYDATABASE_TABLE_plan + " ("+ KEY_CONTENT_plan + " INTEGER not null)";
private SQLiteHelper sqLiteHelper;
private SQLiteDatabase sqLiteDatabase;
private Context context;
int s=0;
byte[]app_image=null;
public SQLiteAdapter(Context c){
context = c;
}
@SuppressWarnings("null")
public void DropDB() {
SQLiteDatabase db = null;
//On peut faire ce qu'on veut ici moi j'ai décidé de supprimer la table et de la recréer
//comme ça lorsque je change la version les id repartent de 0
db.execSQL("DROP DATABASE " + MYDATABASE_NAME + ";");
}
public SQLiteAdapter openToRead() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
return this;
}
public Bitmap getIcone(){
String[] columns = new String[]{KEY_CONTENT_photo};
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getReadableDatabase();
byte[]image_bytes=null;
Cursor rslt=null;
rslt=sqLiteDatabase.query(MYDATABASE_TABLE_photo,columns, null, null, null, null, null);
if(rslt.getCount()!=0){
rslt.moveToFirst();
image_bytes=rslt.getBlob(rslt.getColumnIndex(KEY_CONTENT_photo));
Bitmap bmp=BitmapFactory.decodeByteArray(image_bytes, 0, image_bytes.length);
return bmp;
}
else {
return null;
}
}
public String queueAll(){
String[] columns = new String[]{KEY_CONTENT};
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE_APP, columns,
null, null, null, null, null);
String result = "";
int index_CONTENT = cursor.getColumnIndex(KEY_CONTENT);
for(cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()){
result = result + cursor.getString(index_CONTENT) + "\n";
}
return result;
}
public SQLiteAdapter openToWrite() throws android.database.SQLException {
sqLiteHelper = new SQLiteHelper(context, MYDATABASE_NAME, null, MYDATABASE_VERSION);
sqLiteDatabase = sqLiteHelper.getWritableDatabase();
return this;
}
public void close(){
sqLiteHelper.close();
}
public long insert(String content){
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_CONTENT, content);
return sqLiteDatabase.insert(MYDATABASE_TABLE_APP, null, contentValues);
}
public long insertphoto(byte[] iconebyte){
// "create table " + MYDATABASE_TABLE_APP + " ("+ KEY_CONTENT + " text not null)" +
//" ("+ KEY_CONTENT_ID + " INTEGER not null);"+" ("+ KEY_CONTENT_plan + " INTEGER not null)"+
// String sql="Insert into MYDATABASE_TABLE_APP_photo(KEY_CONTENT_ID_photo,KEY_CONTENT_ID,KEY_CONTENT_photo) values(?,?,?)";
//executeSql
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_CONTENT_photo, iconebyte);
return sqLiteDatabase.insert(MYDATABASE_TABLE_photo, null, contentValues);
}
public int deleteAll(){
return sqLiteDatabase.delete(MYDATABASE_TABLE_APP, null, null);
}
public int deleteAllphoto(){
return sqLiteDatabase.delete(MYDATABASE_TABLE_photo, null, null);
}
@SuppressWarnings("null")
public List<Bitmap> queueAllphoto(){
Bitmap bitmap;
String[] columns = new String[]{KEY_CONTENT_photo};
Cursor cursor = sqLiteDatabase.query(MYDATABASE_TABLE_photo, columns,
null, null, null, null, null);
byte[] result = null;
List<Bitmap> listbitmap=null;
int index_CONTENT = cursor.getColumnIndex(KEY_CONTENT_photo);
for(cursor.moveToFirst(); !(cursor.isAfterLast()); cursor.moveToNext()){
result = cursor.getBlob(index_CONTENT) ;
}
bitmap=BitmapFactory.decodeByteArray(result , 0, result.length);
listbitmap.add(bitmap);
return listbitmap;
}
public class SQLiteHelper extends SQLiteOpenHelper {
public SQLiteHelper(Context context, String name,
CursorFactory factory, int version) {
super(context, name, factory, version);
}
@Override
public void onCreate(SQLiteDatabase db) {
//db.execSQL(SCRIPT_CREATE_DATABASE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
}
}
答案 0 :(得分:0)
首先要知道的是需要显示的blob的数量然后我创建了相同数量的按钮,我只是“setBackgroundRessource”! 这里有一个例子,但图像不是blob而是drawables,但想法是一样的 除了你需要在bytearray中转换blob
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button btn = new Button(getBaseContext());
btn.setId(k1);
final int id_ = btn.getId();
LinearLayout linear = (LinearLayout) findViewById(R.id.linearscroll);
linear.addView(btn, params);
final Button btn1 = ((Button) findViewById(id_));
btn1.setBackgroundResource(matable[k1]);
matable只是我所有可绘制的数组