在 ViewFlipper 中显示图像数据库

show Image database in ViewFlipper

我必须按下按钮:第一个按钮用于将图像从图库保存到数据库,第二个按钮用于在 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++;
        }
    }

logcat 错误:

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 -1是由于getColumnIndex在这一行table中找不到列名造成的String path = cursor2.getString(cursor2.getColumnIndex(imageColumnName));

imageColumnName 未解析为 image,由提取游标的查询选择的列,根据 selectimage..

您或许可以更改 :-

    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 table names, columns and database names and always using those rather than hard coding such names throughout the code, so that there is just the one definition. Doing such can reduce/eliminate such easily made errors.