gpt4 book ai didi

android - 在 ViewFlipper 中显示图像数据库

转载 作者:搜寻专家 更新时间:2023-10-30 22:28:22 24 4
gpt4 key购买 nike

我必须按下按钮:第一个按钮用于将图像从图库保存到数据库,第二个按钮用于在 viewFlipper 中显示来自数据库的图像,但第二个按钮不起作用并且有错误

第一个按钮:

public void save(View view)
{
if(bitmap != null) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 0, stream);
byte[] byteImage = stream.toByteArray();

ContentValues values = new ContentValues();
values.put(imageColumnName, String.valueOf(byteImage));

db.insert(tableName, null, values);

Toast.makeText(this, "Save", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(this, "Save Image First!", Toast.LENGTH_SHORT).show();
}


}

第二个按钮:

public void showImage(View view)
{
ViewFlipper viewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper);
viewFlipper.setFlipInterval(2500);
viewFlipper.startFlipping();

cursor = db.rawQuery(Query_Select_All , null);
int i = 1;
if(cursor.getCount() != 0)
while (cursor.moveToNext())
{
Cursor cursor2 = db.rawQuery("select image from imageColumns where id = "+i , null);
String path = cursor2.getString(cursor2.getColumnIndex(imageColumnName));
File imageFile = new File(path);
Bitmap bitmapImage = BitmapFactory.decodeFile(imageFile.toString());
ImageView imageView = new ImageView(this);
imageView.setImageBitmap(bitmapImage);
viewFlipper.addView(imageView);
i++;
}
}

日志错误:

java.lang.IllegalStateException: Could not execute method for android:onClick   
Caused by: java.lang.reflect.InvocationTargetException
Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 1

最佳答案

Index -1getColumnIndex这一行没有找到表中的列名引起的 String path = cursor2.getString(cursor2.getColumnIndex(imageColumnName ));

imageColumnName 不解析为 image,根据 selectselect,提取游标的查询选择的列strong>image..

你也许可以改变:-

    Cursor cursor2 = db.rawQuery("select image from imageColumns where id = "+i , null);

    Cursor cursor2 = db.rawQuery("select " + imageColumnName + " from imageColumns where id = "+i , null); // The cursor includes just the one column

或到

    Cursor cursor2 = db.rawQuery("select * from imageColumns where id = "+i , null); // The cursor includes all columns (most flexible)

因此,以下可能会起作用:-

public void showImage(View view)
{
ViewFlipper viewFlipper = (ViewFlipper)findViewById(R.id.viewFlipper);
viewFlipper.setFlipInterval(2500);
viewFlipper.startFlipping();

cursor = db.rawQuery(Query_Select_All , null);
int i = 1;
if(cursor.getCount() != 0)
while (cursor.moveToNext())
{
Cursor cursor2 = db.rawQuery("select * from imageColumns where id = "+i , null);
String path = cursor2.getString(cursor2.getColumnIndex(imageColumnName));
File imageFile = new File(path);
Bitmap bitmapImage = BitmapFactory.decodeFile(imageFile.toString());
ImageView imageView = new ImageView(this);
imageView.setImageBitmap(bitmapImage);
viewFlipper.addView(imageView);
i++;
}
}

注意事项

  • 可能和可能的使用是因为列的实际名称在提供的代码中不清楚。

I and perhaps many others, would recommend creating constants in the respective class for tablenames, columns and database names and always using those rather thanhard coding such names throughout the code, so that there is just theone definition. Doing such can reduce/eliminate such easily madeerrors.

关于android - 在 ViewFlipper 中显示图像数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48118686/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com