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