1

I created a simple app in which the image store from the ImageView to the database. But when click on the retrive button is show that index 1 requested with size of 3. I don't know what thing is going wrong. database class:

           @Override
    public void onCreate(SQLiteDatabase db) {
    String CREATE_IMAGE_TABLE = "CREATE TABLE " +TABLE_NAME  + "("
            + IMAGE_KEY + " BLOB )";
     db.execSQL(CREATE_IMAGE_TABLE);
     }
        @Override
       public void onUpgrade(SQLiteDatabase db, int i, int i1) {
     db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
     onCreate(db);

    }
     public  boolean insertData(byte[]image )throws SQLiteException
     {
    SQLiteDatabase db = this.getWritableDatabase();


    ContentValues cv=new ContentValues();
    cv.put(IMAGE_KEY,image);
    long result= db.insert(TABLE_NAME,null,cv);
    if(result==-1)
        return false;
    else
        return true;

   }
  public Cursor getAllData()
  {
    SQLiteDatabase db=this.getReadableDatabase();
    Cursor res=db.rawQuery("select * from "+TABLE_NAME,null);
    byte[]img=res.getBlob(0);
    return res;
  }

And this is the activity class:

           public void button2(View view)
            {
            try {

             Cursor res = myDb.getAllData();
            if (res ==null) {
            showMessage("error", "no data found");
            } else {
            StringBuffer buffer = new StringBuffer();
            while (res.moveToNext()) {
                buffer.append("id:" + res.getBlob(0) + "\n");
                byte[] image = res.getBlob(0);

                Bitmap bmp = BitmapFactory.decodeByteArray(image, 0,
                                 image.length); 


                imagee.setImageBitmap(bmp);

            }
            // showMessage("DATA", buffer.toString());
            }
           }
            catch (Exception e)
            {
          Toast.makeText(getBaseContext(),e.getMessage(),
                  Toast.LENGTH_LONG).show();
                    }}

              public  void buttonn(View view)
     {
      Bitmap bitmap = ((BitmapDrawable) imagee.getDrawable()).getBitmap();
      ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
      bitmap.compress(Bitmap.CompressFormat.PNG, 0, outputStream);
      byte[] data = outputStream.toByteArray();
            boolean isInserted = myDb.insertData(data);

          if (isInserted == true)
        Toast.makeText(getBaseContext(), "Registration Succes!",
              Toast.LENGTH_SHORT).show();
            else 

           Toast.makeText(getBaseContext(), "No Record Registered!",
               Toast.LENGTH_SHORT).show();     

                }
                  }

I tried most but couldn't do not thing.I change it from res.movetoNext but show the same error and use res.movetoFirst it also show the same error

2 Answers2

2

Android sqlite CursorWindow has a fixed size buffer that is 2MB on most configurations. You cannot move around rows any larger than that.

Don't store large binary data such as images in Android sqlite. Use external storage instead and just save the path in your database.

laalto
  • 150,114
  • 66
  • 286
  • 303
0

Check this code about how to save an image in android:

private String saveToInternalStorage(Bitmap bitmapImage){
        ContextWrapper cw = new ContextWrapper(getApplicationContext());
         // path to /data/data/yourapp/app_data/imageDir
        File directory = cw.getDir("imageDir", Context.MODE_PRIVATE);
        // Create imageDir
        File mypath=new File(directory,"profile.jpg");

        FileOutputStream fos = null;
        try {           
            fos = new FileOutputStream(mypath);
       // Use the compress method on the BitMap object to write image to the OutputStream
            bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
        } catch (Exception e) {
              e.printStackTrace();
        } finally {
              fos.close(); 
        } 
        return directory.getAbsolutePath();
    }

You need just to add the path of your image into your database instead of the blob image.

Reference: Saving and Reading Bitmaps/Images from Internal memory in Android

Community
  • 1
  • 1
aurelianr
  • 538
  • 2
  • 12
  • 35