0

i m facing this error while saving image in sqlite database , image size is only 240KB i have gone through some solutions but could not solve this problem my code for saving and getting image in DB

public class MainActivity extends AppCompatActivity {

SQLiteDatabase db;
ImageView img,img_get;
Button btn_save,btn_get;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    img=(ImageView)findViewById(R.id.img_save);
    img_get=(ImageView)findViewById(R.id.img_get);
    btn_get=(Button)findViewById(R.id.btn_get);
    btn_save=(Button)findViewById(R.id.btn_save);
    db= openOrCreateDatabase("Mydb", MODE_PRIVATE, null);
    db.execSQL("create table if not exists hello(name varchar, a blob)");


    btn_save.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
         saveimage(v);

        }
    });

    btn_get.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v)
        {
            getimage(v);
        }
    });
}
public void saveimage(View view)
{
    byte[] image=new byte[R.drawable.red];
    ContentValues value=new ContentValues();
    value.put("name","hatib abrar");
    value.put("a",image);
    db.insert("hello",null,value);
    Toast.makeText(getApplicationContext(),"Saved",Toast.LENGTH_SHORT).show();
}
public void getimage(View view)
{
    Cursor c=db.rawQuery("select * from hello",null);
    if (c.moveToNext())
    {
        String name=c.getString(0);
        byte[] image=c.getBlob(1);
        Bitmap bm= BitmapFactory.decodeByteArray(image,0,image.length);
        img_get.setImageBitmap(bm);
        Toast.makeText(getApplicationContext(),"Error getting image",Toast.LENGTH_SHORT).show();
    }

}

LogCat is showing this error Throwing

OutOfMemoryError "Failed to allocate a 2130837597 byte allocation with 16777216 free bytes and 151MB until OOM" 09-07 09:49:59.651 21823-21823/com.example.e6530.imagesaving D/AndroidRuntime: Shutting down VM 09-07 09:49:59.651 21823-21823/com.example.e6530.imagesaving E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.e6530.imagesaving, PID: 21823 java.lang.OutOfMemoryError: Failed to allocate a 2130837597 byte allocation with 16777216 free bytes and 151MB until OOM at com.example.e6530.imagesaving.MainActivity.saveimage(MainActivity.java:51) at com.example.e6530.imagesaving.MainActivity$1.onClick(MainActivity.java:36) at android.view.View.performClick(View.java:4793) at android.view.View$PerformClick.run(View.java:19971) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:135) at android.app.ActivityThread.main(ActivityThread.java:5669) 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:960) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

Prashant
  • 1,593
  • 1
  • 17
  • 32
hatib abrar
  • 123
  • 3
  • 14

2 Answers2

2

Your problem is in this line:

byte[] image=new byte[R.drawable.red];

Often R.drawable is some big integer. For example 1billion, so you try to create array of billion bytes. And it's a lot of memory. Please, do not pass resource id as size of array.

Updated

Please, see this answer and this page to handle how you can retrieve bitmap from resource and convert it to byte array. Probably you need to write something like:

Resource r = ...;
Bitmap bitmap = Bitmap.decodeResource(r, R.drawable.red);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] bitmapdata = stream.toByteArray();
Community
  • 1
  • 1
Michael Spitsin
  • 2,539
  • 2
  • 19
  • 29
2

This:

byte[] image=new byte[R.drawable.red];

does not do what you think it does. R.drawable.red is a generated integer ID, which can be quite high. In your case something like 2130837597, which is the amount of bytes you try to allocate.

You have to first load your image, then get its bytes and load these into your database. A quick search comes up with this for example.

Community
  • 1
  • 1
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195