我想在 ViewFlipper 的图像视图中添加存储在数据库中的图像。我试过这样做,但只显示最后存储的图像,但不是全部。我找不到我在哪里犯了错误。这是我的代码,请帮助我。我是安卓新手。
数据库中的所有图像都没有显示在 viewflipper 中。viewflipper 中仅显示数据库中的最后一张图像。我希望数据库中的所有图像都显示在 viewflipper 中。
MainActivity.java
public class MainActivity extends AppCompatActivity {
DatabaseHelper myDb;
EditText editImagename;
Button btnAddData;
Button btnview;
ImageView imgview;
ViewFlipper viewflips;
private static int RESULT_LOAD_IMAGE = 1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
editImagename = findViewById(R.id.image_name);
btnAddData = findViewById(R.id.addbtn);
btnview = findViewById(R.id.getbtn);
imgview = new ImageView(this);
viewflips = findViewById(R.id.viewflip);
viewAll();
Button buttonLoadImage = findViewById(R.id.btn1);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent i = new Intent(Intent.ACTION_PICK,Uri.parse("content://media/internal/images/media"));
startActivityForResult(i, RESULT_LOAD_IMAGE);
}
});
myDb = new DatabaseHelper(this);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK) {
Uri uri = data.getData();
final String x = getPath(uri);
final String ImageName = editImagename.getText().toString();
btnAddData.setOnClickListener(
new View.OnClickListener(){
public void onClick(View v){
boolean isInserted = myDb.insertData(ImageName, x);
if(isInserted == true)
Toast.makeText(MainActivity.this,"Data Inserted",Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this,"Data not Inserted",Toast.LENGTH_LONG).show();
}
}
);
}
}
private String getPath(Uri uri) {
if (uri==null) return null;
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(uri,projection,null,null,null);
if (cursor!=null){
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
return uri.getPath();
}
public void viewAll() {
btnview.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor res = myDb.getAllData();
if (res.getCount()!=0)
while(res.moveToNext()){
Uri mUri = Uri.parse(res.getString(2));
imgview.setImageURI(mUri);
viewflips.removeView(imgview);
viewflips.addView(imgview);
}
}
}
);
}
}
数据库助手.java
public class DatabaseHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Flight.db";
public static final String TABLE_NAME = "flight_table";
public static final String COL_1 = "ImageName";
public static final String COL_2 = "Image";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table " + TABLE_NAME + " (ID INTEGER PRIMARY KEY AUTOINCREMENT,ImageName TEXT,Image BLOB)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean insertData(String ImageName, String x) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1,ImageName);
contentValues.put(COL_2,x);
db.insert(TABLE_NAME,null ,contentValues);
return true;
}
Cursor getAllData() {
SQLiteDatabase db = this.getWritableDatabase();
Cursor res = db.rawQuery("select * from "+TABLE_NAME,null);
return res;
}
}