0

I am converting my imageview to byte it will work fine when i select min size images i.e. 20kb-300kb approx but when i select image captured by mobile camera it shows the error.

private byte[] imageViewToByte(ImageView image) 
{
    Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    byte[] byteArray = stream.toByteArray();
    return byteArray;
} 

My sql query is

sqLiteHelper.queryData("CREATE TABLE IF NOT EXISTS FOOD(Id INTEGER PRIMARY KEY AUTOINCREMENT, name DROPDOWN, price VARCHAR, image BLOB)");

At the decode image i use

byte[] foodImage = food.getImage();
    Bitmap bitmap = BitmapFactory.decodeByteArray(foodImage, 0, foodImage.length);
    holder.imageView.setImageBitmap(bitmap);

And my cursor for getting data is

Cursor cursor = MainActivity.sqLiteHelper.getData("SELECT * FROM FOOD");
    list.clear();
    while (cursor.moveToNext()){
        int id = cursor.getInt(0);
        // int id = cursor.getInt(cursor.getColumnIndex("Id"));
        String name = cursor.getString(1);
        String price = cursor.getString(2);
        byte[] image = cursor.getBlob(3);

        list.add(new Notice(id,name, price, image));
    }

And i got the error i.e.

E/AndroidRuntime: FATAL EXCEPTION: main
              Process: com.project.sumit.diems_onlinenoticeboard, PID: 4122
              java.lang.RuntimeException: Unable to start activity ComponentInfo{com.project.sumit.diems_onlinenoticeboard/com.project.sumit.diems_onlinenoticeboard.NoticeActivityAll}: java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2326)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                  at android.app.ActivityThread.access$800(ActivityThread.java:147)
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
                  at android.os.Handler.dispatchMessage(Handler.java:102)
                  at android.os.Looper.loop(Looper.java:135)
                  at android.app.ActivityThread.main(ActivityThread.java:5264)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695)
               Caused by: java.lang.IllegalStateException: Couldn't read row 0, col 0 from CursorWindow.  Make sure the Cursor is initialized correctly before accessing data from it.
                  at android.database.CursorWindow.nativeGetLong(Native Method)
                  at android.database.CursorWindow.getLong(CursorWindow.java:511)
                  at android.database.CursorWindow.getInt(CursorWindow.java:578)
                  at android.database.AbstractWindowedCursor.getInt(AbstractWindowedCursor.java:69)
                  at com.project.sumit.diems_onlinenoticeboard.NoticeActivityAll.onCreate(NoticeActivityAll.java:58)
                  at android.app.Activity.performCreate(Activity.java:5975)
                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                  at android.app.ActivityThread.access$800(ActivityThread.java:147) 
                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281) 
                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                  at android.os.Looper.loop(Looper.java:135) 
                  at android.app.ActivityThread.main(ActivityThread.java:5264) 
                  at java.lang.reflect.Method.invoke(Native Method) 
                  at java.lang.reflect.Method.invoke(Method.java:372) 
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:900) 
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:695) 

Plz help mee, i am trying to solve this issue from to many days but i can't done it.

Sumit
  • 70
  • 1
  • 8
  • Maybe this can help? Looks like there is Cursor size limit http://stackoverflow.com/questions/5406429/cursor-size-limit-in-android-sqlitedatabase https://github.com/ankidroid/Anki-Android/pull/4033 – mrsq Mar 24 '17 at 18:53
  • I would suggest storing the files in a storage folder on the device and store the file paths in the SQLite database then use the paths to read the images from the device's file storage – Chester Cobus Mar 24 '17 at 18:55
  • sir, can you give me sample code for that, because i started learning android a month ago so i don't know how to do that. @Chester Cobus and thank you so much. – Sumit Mar 24 '17 at 19:08

1 Answers1

0

Writing file:

private String saveFile(Bitmap imageToSave, String fileName) {

File file = null;

  try {
    file = new File(new File(Environment.getExternalStorageDirectory() + "/Food"), fileName);
    FileOutputStream out = new FileOutputStream(file);
    imageToSave.compress(Bitmap.CompressFormat.JPEG, 100, out);
    out.flush();
    out.close();

  } catch (Exception e) {
    e.printStackTrace();
  }

  return file.getAbsolutePath();
}

Reading file:

BitmapFactory.Options options = new BitmapFactory.Options();

options.inPreferredConfig = Bitmap.Config.ARGB_8888;

Bitmap bitmap = BitmapFactory.decodeFile(food.getImageUrl(), options);
holder.imageView.setImageBitmap(bitmap);

Don't forget to add these permissions to AndroidManifest.xml:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

SQLite query updated:

sqLiteHelper.queryData("CREATE TABLE IF NOT EXISTS FOOD(Id INTEGER PRIMARY KEY AUTOINCREMENT, name DROPDOWN, price VARCHAR, imageUrl VARCHAR)");

Update your Notice object to have imageUrl then cursor code looks like this:

Cursor cursor = MainActivity.sqLiteHelper.getData("SELECT * FROM FOOD");
list.clear();
while (cursor.moveToNext()){
    int id = cursor.getInt(0);
    // int id = cursor.getInt(cursor.getColumnIndex("Id"));
    String name = cursor.getString(1);
    String price = cursor.getString(2);
    String imageUrl = cursor.getString(3);

    list.add(new Notice(id,name, price, imageUrl));
}
Chester Cobus
  • 701
  • 4
  • 12
  • what changes i will do in that sqLiteHelper.insertData( selectbranch.getSelectedItem().toString().trim(), edtPrice.getText().toString().trim(), imageViewToByte(imageView) ); – Sumit Mar 25 '17 at 03:28
  • sqLiteHelper.insertData( selectbranch.getSelectedItem().toString().trim(), edtPrice.getText().toString().trim(), saveFile(imageView, "IMG_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()))); – Chester Cobus Mar 25 '17 at 10:52
  • imageView shows an error i.e. Wrong 1st argument type. Found: 'android.widget.ImageView', required: 'android.graphics.Bitmap' saveFile (android.graphics.Bitmap, String) in MainActivity cannot be applied to (android.widget.ImageView, String) – Sumit Mar 25 '17 at 14:21
  • sqLiteHelper.insertData( selectbranch.getSelectedItem().toString().trim(), edtPrice.getText().toString().trim(), saveFile(((BitmapDrawable)image.getDrawable()).getBitmap(), "IMG_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()))); – Chester Cobus Mar 25 '17 at 14:28
  • image.getDrawable() first image gives error that multiple choice i import R.id.Image then getDrawable method gives error and said add qualifier imageView to method when i pressed alt+enter – Sumit Mar 25 '17 at 16:07
  • sqLiteHelper.insertData( selectbranch.getSelectedItem().toString().trim(), edtPrice.getText().toString().trim(), saveFile(((BitmapDrawable)imageView.getDrawable()).getBitmap(), "IMG_" + new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date()))); – Chester Cobus Mar 25 '17 at 16:38
  • it's imageView not image sorry – Chester Cobus Mar 25 '17 at 16:39
  • Wrong 3rd argument type. found: 'java.lang.String'. required 'byte[]' – Sumit Mar 25 '17 at 16:43
  • please refactor your code by moving them into variables so you can see your argument issues. – Chester Cobus Mar 25 '17 at 17:37
  • Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/Food/IMG_20170326_103104: open failed: ENOENT (No such file or directory) – Sumit Mar 26 '17 at 05:03
  • sir, i refactor code then pressed alt+enter then error gone and my application run but at the run time file not found exception is occurred and it is not shown in which activity this exception is present, – Sumit Mar 26 '17 at 05:05
  • http://stackoverflow.com/questions/25202977/how-to-create-android-directory-automatically-if-it-doesnt-already-exist – Chester Cobus Mar 26 '17 at 12:29
  • I create directory manually in my mobile and afterword it works finally. Thank you so much sir... – Sumit Mar 26 '17 at 14:03
  • Now directory is also automatically created by referring above question thanks again. – Sumit Mar 26 '17 at 14:31