How to use Bitmap to store pictures into database

Posted by bretx on Sun, 17 Nov 2019 17:12:28 +0100

This is my first blog, please give me more advice!

About a month ago, in the process of developing an APP with friends, we found a problem: image storage. Because there is no image data type in the database, we can't directly put the uploaded image into the database when the user needs to store it.

After several days of exploration, combined with Guo Shen's "the second line of code" to call the camera to take photos and select pictures from the album, we found a picture class in Android: Bitmap. Finally, it is found that Bitmap and its related tool classes can be used to store and display images.  

Main tool classes:
     

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Base64;
 
import java.io.ByteArrayOutputStream;
 
/**
 * Created by cartoon on 2017/12/9.
 */
 
public class StringAndBitmap {
    //The conversion between pictures and strings is convenient for storing pictures in the database
    private Bitmap bitmap;
    private String string;
    public Bitmap stringToBitmap(String string){
        //String type in database converted to Bitmap
        if(string!=null){
            byte[] bytes= Base64.decode(string,Base64.DEFAULT);
            bitmap= BitmapFactory.decodeByteArray(bytes,0,bytes.length);
            return bitmap;
        }
        else {
            return null;
        }
    }
    public String bitmapToString(Bitmap bitmap){
        //The pictures uploaded by the user in the activity are converted into String for storage
        if(bitmap!=null){
            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
            byte[] bytes = stream.toByteArray();// Convert to byte array
            string=Base64.encodeToString(bytes,Base64.DEFAULT);
            return string;
        }
        else{
            return "";
        }
    }
}

The String statement string, which has been stored in the database, has been acquired below. It only calls for the method of displaying Bitmap in the component that needs to display the picture.

imageView.setImageBitmap(stringAndBitmap.stringToBitmap(string);
//Here, imageView is the ID of the page component binding, and string is the string form of the image obtained from the database

This is required to store images uploaded by users.

bitmap=((BitmapDrawable)imageView.getDrawable()).getBitmap();
string=stringAndBitmap.bitmapToString(bitmap);

After some database operations, that is, the images uploaded by users can be stored in the database.

Because I am not responsible for the database part, my suggestion is to select BLOB (MySQL) as the type in the database, because it has been implemented and is feasible.

The above is a little skill developed before, which is also learned through pain. We haven't tested the consumption and delay of resources, but we can store pictures in the database.

If you have any suggestions or comments on this post, please feel free to write privately or comment below. The most important thing is to be able to help the entrants like us.

Topics: Android Database Java MySQL