Convert Drawable to BLOB Datatype

后端 未结 3 1260
执念已碎
执念已碎 2020-12-06 03:32
public Drawable icon; \\\\ Some value in this field

I am creating a database in SQLite Manager. I want to store icon into the database

相关标签:
3条回答
  • 2020-12-06 04:13

    You can convert your image into byte array and store that bytearray in blob filed in database. You can retrieve your image back from database as byte array.

    How to get byte array from imageview:

    ImageView iv = (ImageView) findViewById(R.id.splashImageView);
    Drawable d = iv.getBackground();
    BitmapDrawable bitDw = ((BitmapDrawable) d);
    Bitmap bitmap = bitDw.getBitmap();
    System.out.println(".....d....."+d);
    System.out.println("...bitDw...."+bitDw);
    System.out.println("....bitmap...."+bitmap);
    ByteArrayOutputStream stream = new ByteArrayOutputStream();
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
    byte[] imageInByte = stream.toByteArray();
    

    Store byte array in database:

    ContentValues cv = new ContentValues();
    cv.put(CHUNK, buffer); //CHUNK blob type field of your table
    long rawId = database.insert(TABLE, null, cv); //TABLE table name
    

    Retrieve image from byte array:

    public static Bitmap convertByteArrayToBitmap(
        byte[] byteArrayToBeCOnvertedIntoBitMap)
    {
        bitMapImage = BitmapFactory.decodeByteArray(
            byteArrayToBeCOnvertedIntoBitMap, 0,
            byteArrayToBeCOnvertedIntoBitMap.length);
        return bitMapImage;
    }
    
    0 讨论(0)
  • 2020-12-06 04:13

    I made few changes in above answer to get byte array imageview

    ImageView iv = (ImageView) findViewById(R.id.splashImageView);

    iv.setDrawingCacheEnabled(true);
            Bitmap bitmap = iv.getDrawingCache();
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
            byte[] bitmapdata = stream.toByteArray();
    
    0 讨论(0)
  • 2020-12-06 04:13

    if you want to save multiple images to database then you can create own method as following : //retrieve images from drawable into int array:

    int[] IMAGES = { R.drawable.yourimage1, R.drawable.yourimage2, R.drawable.yourimage3,
                R.drawable.yourimage4, R.drawable.yourimage5 }; 
    
    //create method to convert drawable images(which are in int) to byte[] as below:
    
    public byte[] convertToByteArray(int image){
    
            Resources resources = getResources();
            Drawable drawable = resources.getDrawable(image);
            Bitmap bitmap =  ((BitmapDrawable)drawable).getBitmap();
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress( Bitmap.CompressFormat.PNG, 100, stream);
            byte[] bitmapData = stream.toByteArray();
    
                  return bitmapData;
    
        }
    

    and now call this method as below: byte [] image = convertToByteArray(IMAGES[position]);// dbAdapter.insertValues(image);

    0 讨论(0)
提交回复
热议问题