i have to button : first button is for save image from gallery to database , second button is for show images from database in viewFlipper but second button dont work and have error
first button :
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();
}
}
second button :
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 error :
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 is caused by getColumnIndex
not finding the column name in the table in this line String path = cursor2.getString(cursor2.getColumnIndex(imageColumnName));
That is imageColumnName
does not resolve to image
, the column that is selected by the query that extracts the cursor, as per select
image
..
You could perhaps change :-
Cursor cursor2 = db.rawQuery("select image from imageColumns where id = "+i , null);
to
Cursor cursor2 = db.rawQuery("select " + imageColumnName + " from imageColumns where id = "+i , null); // The cursor includes just the one column
or to
Cursor cursor2 = db.rawQuery("select * from imageColumns where id = "+i , null); // The cursor includes all columns (most flexible)
As such the following would likely work :-
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.