Raed Ghazal
Raed Ghazal

Reputation: 115

App crash when selecting more than 10 images

I am coding an android app using android studio - java , I am facing a problem when picking more than 10 images from gallery and inserting them into Sqlitedatabase table , when inserting the 10th image it takes much longer time than the last 9 images , and when selecting all the images (10) from the table the app crashes with errors

E/SQLiteCursor: onMove() return false. RequiredPos: 9 WindowStartPos: 9 WindowRowCount: 0(original count of query: 10)
Caused by: android.database.CursorIndexOutOfBoundsException: Index -1 requested, with a size of 10

Code

Picking and inserting images code

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode== 1  && resultCode == RESULT_OK) {
        try {
            ClipData clipData = data.getClipData();
            if (clipData == null)
                return;
            for (int i = 0; i < clipData.getItemCount(); i++) {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), clipData.getItemAt(i).getUri());
                byte[] myImg = getBitmapAsByteArray(bitmap);
                DatabaseHelper databaseHelper = new DatabaseHelper(this);
                SQLiteDatabase db = databaseHelper.getWritableDatabase();
                ContentValues contentValues = new ContentValues();
                contentValues.put("myImage",myImg);
                db.insert("myTable",null,contentValues);
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

getBitmapAsByteArray()

 private static byte[] getBitmapAsByteArray(Bitmap bitmap) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.PNG, 0, outputStream);
    return outputStream.toByteArray();
}

Selecting images from database

 public void showImages(View view) {
    DatabaseHelper databaseHelper = new DatabaseHelper(this);
    SQLiteDatabase db = databaseHelper.getReadableDatabase();
    Cursor cursor = db.rawQuery("select * from myTable",null);
    cursor.moveToFirst();
    Toast.makeText(this, ""+cursor.getCount(), Toast.LENGTH_SHORT).show();
    ArrayList<Bitmap> bitmaps = new ArrayList<>();
    if (cursor.getCount()>0)
        while (!cursor.isAfterLast())
        {
            bitmaps.add(BitmapFactory.decodeByteArray(cursor.getBlob(0), 0, cursor.getBlob(0).length));
            cursor.moveToNext();
        }

    ViewPagerAdapter adapter = new ViewPagerAdapter(this,bitmaps);
    viewPager.setAdapter(adapter);
    viewPager.setCurrentItem(0);
    cursor.close();
}

any suggestions ?

Upvotes: 0

Views: 329

Answers (2)

Jemshit
Jemshit

Reputation: 10048

Not sure about the code, but i would not store image directly in Database. Image can be really huge, it might take some time to write and read multiple images.

Instead, like what backend developers do, i would store image as File on Filesystem (app private directory). They will be deleted when app is deleted. Save the images as Files and store their path or name into DB. So whenever you need image, just do:

  1. List<String> getImageNamesFromDb()
  2. List<File> getImageFiles(List<String>)
  3. processYourImageFiles(List<File>)

Upvotes: 1

Mohd Akbar
Mohd Akbar

Reputation: 111

It is an index out of bound exception. Your loop is going in negative , you are accessing a row at index -1 which does not exist. Try errors in your loop. Please give your code of java file. It might help more for finding more errors.

Upvotes: 0

Related Questions