1
ImageView img1;
    img1 = (ImageView) findViewById (R.id.img1);

        URL newurl = new URL("http://10.0.2.2:80/Gallardo/Practice/files/images/donut.jpg");
        Bitmap mIcon_val = BitmapFactory.decodeStream(newurl.openConnection() .getInputStream());
        img1.setImageBitmap(mIcon_val);

I get image from url but I want to store it to my drawable, how?

3 Answers3

1

Everything in the apk will be read only. So you can't write to drawable.

you have to use "blob" to store image.

ex: to store a image in to db

public void insertImg(int id , Bitmap img ) {   


    byte[] data = getBitmapAsByteArray(img); // this is a function

    insertStatement_logo.bindLong(1, id);       
    insertStatement_logo.bindBlob(2, data);

    insertStatement_logo.executeInsert();
    insertStatement_logo.clearBindings() ;

}

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

to retrieve a image from db

public Bitmap getImage(int i){

    String qu = "select img  from table where feedid=" + i ;
    Cursor cur = db.rawQuery(qu, null);

    if (cur.moveToFirst()){
        byte[] imgByte = cur.getBlob(0);
        cur.close();
        return BitmapFactory.decodeByteArray(imgByte, 0, imgByte.length);
    }
    if (cur != null && !cur.isClosed()) {
        cur.close();
    }       

    return null ;
} 

You can also check this saving image to database

Setu Kumar Basak
  • 11,460
  • 9
  • 53
  • 85
  • Sir may I ask what is this line? insertStatement_logo.bindLong(1, id); insertStatement_logo.bindBlob(2, data); insertStatement_logo.executeInsert(); insertStatement_logo.clearBindings() ; – Majikero Gallardo Apr 09 '15 at 15:20
1

You can construct Drawable by using this constructor:

Drawable drawable = new BitmapDrawable(getResources(), mIcon_val);

You can set it to the ImageView like so:

img1.setImageDrawable(drawable);
wavemode
  • 2,076
  • 1
  • 19
  • 24
0

Its not possible to place image in drawable folder while running the apk.. Saving an image to SDcard is possible..

Anitha
  • 253
  • 4
  • 16
  • How about database? SQLite? – Majikero Gallardo Apr 09 '15 at 05:04
  • yes possible try http://stackoverflow.com/questions/9357668/how-to-store-image-in-sqlite-database , http://stackoverflow.com/questions/19538966/how-to-store-an-image-in-the-database-android , http://www.coderzheaven.com/2012/12/23/store-image-android-sqlite-retrieve-it/ – Anitha Apr 09 '15 at 05:07