1

I have inserted capture image by using code below and get "not null" which indicates the data is not null.

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View arg0) {

                if ((name != null && name.trim().length() > 0) && (result != null && result.trim().length() > 0)) {
                   // Toast.makeText(getActivity().getApplicationContext(), fk+"", Toast.LENGTH_LONG).show();
                    byte[] data=getBitmapAsByteArray(Global.img); // this is a function
                    if(data==null)
                    {
                        Toast.makeText(getActivity(), "null", Toast.LENGTH_LONG).show();
                    }
                    else
                    {


                        Toast.makeText(getActivity(), " not null", Toast.LENGTH_LONG).show();
                        SB.insertStaffBenefit(name, data, description, result, fk);
                    }
                }

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

However, app crashed when I try to retrieve the captured image out.

 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.receipt);
    dbHelper = new MyDatabaseHelper(this);
    final String k = getIntent().getExtras().getString("ID");
    Toast.makeText(getApplicationContext(), k+"", Toast.LENGTH_LONG).show();
     ImageView a=(ImageView)findViewById(R.id.imageView5);
     database = dbHelper.getWritableDatabase();
     c = database.rawQuery("SELECT s.Image FROM Information i LEFT JOIN StaffBenefit s ON s.Twd_id=i._id WHERE i._id=? ", new String[]{String.valueOf(k)},null);
                if(c.moveToFirst())
                        {
                       byte[] img=c.getBlob(c.getColumnIndexOrThrow("Image"));
                        if(img!=null)
                        {
                            Log.e("TAG", " Not null");
                        }
                        else
                        {
                            Log.e("TAG", " null");
                        }
                        ByteArrayInputStream imageStream = new ByteArrayInputStream(img);
                        Bitmap theImage= BitmapFactory.decodeStream(imageStream);
                        a.setImageBitmap(theImage);

                    }

                c.close();
            }
        }

MyDatabaseHelper.java

 public void onCreate(SQLiteDatabase db) {
        db.execSQL("create table " + TABLE_STAFF_BENEFIT + "( "  + ID3 + " INTEGER PRIMARY KEY , Claim_Type TEXT, Image BLOB, Description TEXT , Amount TEXT, Twd_id INTEGER, FOREIGN KEY(Twd_id) REFERENCES "+TABLE_INFO+"(_id))");
}

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.nativeGetBlob(Native Method) at android.database.CursorWindow.getBlob(CursorWindow.java:404) at android.database.AbstractWindowedCursor.getBlob(AbstractWindowedCursor.java:45) at com.example.project.project.Receipt.onCreate(Receipt.java:40)

(Receipt.java:40)

  byte[] img=c.getBlob(c.getColumnIndexOrThrow("Image"));

Does the error caused by the large blob as stated at Retrieve large blob from Android sqlite database ? How can I solve it ? Thanks

Community
  • 1
  • 1
Hoo
  • 1,806
  • 7
  • 33
  • 66
  • @Hoo Could you post your create table query? – Amsheer Nov 10 '15 at 10:13
  • @Amsheer sure. See the edited. I just show you the table which consists of the `image column` – Hoo Nov 10 '15 at 10:23
  • @Amsheer In my app, the camera function can supports `selected image from gallery` and `take photo`. The inserted image which selected from gallery can be retrieved, but `captured image` cannot. – Hoo Nov 10 '15 at 10:25
  • I didn't find the error. But if you change your logic you can avoid this. Instead of saving image in database just store the path in database. – Amsheer Nov 10 '15 at 10:25
  • @Amsheer How to store the path in database? The `insert` and `retrieve` `code` does not same with `selected image from gallery`? – Hoo Nov 10 '15 at 10:31
  • What i mean was different devices have different image size so I am not sure always BLOB is good. In your case only your Intent capture picture is showing error so you need to check there is an image or not? – Amsheer Nov 10 '15 at 10:36
  • For these I'm suggesting just take picture using the camera and save in your external SD card then save the image path to database. – Amsheer Nov 10 '15 at 10:38
  • @Amsheer ya it has,since it will display `not null` before insert into database. Just not sure whether it has inserted properly or not. – Hoo Nov 10 '15 at 10:38

1 Answers1

0

I think you found the solution by your own.

Do you know how big the Image/Images is/are in the onClick-Handler? If they are bigger than 1Mb you aren't able to hold the Information in one Cursor. See your own link for the solution.

But maybe you can test it first, if it's possible. Try to insert/select an Image which size is below 1Mb and then an Image with a size above 1Mb.

If the first test succeeds and the second doesn't, you need to follow the solution like in your post: Retrieve large blob from Android sqlite database

Community
  • 1
  • 1
  • How can I know the size of captured image? – Hoo Nov 10 '15 at 10:32
  • byte[] data=getBitmapAsByteArray(Global.img); In this line you are saving the captured Image in the local Byte[] Array, am I right? If so, get the length/size of the Array and simply calculate the size in Mb – Christoph Pölsterl Nov 10 '15 at 10:35
  • I'm sorry to bother you again. I get `[B@437dc7e8` for `data`. What should I do next so can calculate it in Mb? – Hoo Nov 10 '15 at 10:50
  • okay, try this: modify your getBitmapAsByteArray(Bitmap bitmap) method and do the following (use console to print out, or Log, what ever you want): Display the value of this Statement: outputStream.size()/1024 //then you have the size in KB – Christoph Pölsterl Nov 10 '15 at 11:04
  • How to modify? And need to add outputStream.size()/1024 at where? – Hoo Nov 10 '15 at 11:12
  • public static byte[] getBitmapAsByteArray(Bitmap bitmap) { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 0, outputStream); System.out.println("Blob size: " + outputStream.size()/1024 + " KB"); return outputStream.toByteArray(); } – Christoph Pölsterl Nov 10 '15 at 11:17
  • yeah sure not, but now we know how large your blob is ... if the Output says that the size is >1024KB then follow the above link to solve the problem – Christoph Pölsterl Nov 10 '15 at 12:04
  • So do I need to have different coding? One for `captured image` and another for `selected image from gallery` – Hoo Nov 10 '15 at 12:06
  • No. The Problem is, that the Courser of the sqlite3 for Android can't hold Information which size is above 1024KB (1MB). Different coding wouldn't change anything. You have three Options: 1. Do it like here: http://stackoverflow.com/questions/12716859/retrieve-large-blob-from-android-sqlite-database 2. Compress the Images to death 3. Save the Image to the SD Card and only save the path to the file in the database – Christoph Pölsterl Nov 10 '15 at 12:19
  • How to Compress the Images to death? – Hoo Nov 10 '15 at 12:21
  • Can I reduce the captured image size? – Hoo Nov 10 '15 at 12:22
  • There is one last thing you can try, but then I am out of knowledge... In the line you are compressing, don't use PNG, because PNG is ignoring Quality Settings. Use JPEG instead and play around with the Quality Settings from 0 to 100 (while 0 is the lowest Quality) – Christoph Pölsterl Nov 10 '15 at 12:34