Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Bitmap as an intermediary to store pictures in the database

Shulou Source: shulou.com Published: 2022-06-02 13:33:54 10月04日 Update

This article will explain in detail how to use Bitmap as an intermediary to store pictures in the database. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

The storage and display of pictures can be realized by using Bitmap and its related tool classes.

The main tool classes used are:

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 {/ / conversion between pictures and String to facilitate the storage of pictures in the database private Bitmap bitmap; private String string Public Bitmap stringToBitmap (String string) {/ / String types in the database are converted to Bitmap if (stringbroken 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 users during the activity are converted into String to store if (bitmappings null) {ByteArrayOutputStream stream = new ByteArrayOutputStream (); bitmap.compress (Bitmap.CompressFormat.PNG, 100,100, stream); byte [] bytes = stream.toByteArray (); / / converted to byte array string=Base64.encodeToString (bytes,Base64.DEFAULT) Return string;} else {return ";}

Now that you have obtained the String statement string of the image that has been stored in the database, you only need to call the method to display Bitmap in the component that needs to display the picture.

ImageView.setImageBitmap (stringAndBitmap.stringToBitmap (string); / / the imageView here binds the ID,string to the page component as the string form of the image obtained from the database

This is needed to store the pictures uploaded by the user.

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

After some database operations, the pictures uploaded by users can be stored in the database.

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

This is the end of the article on "how to use Bitmap as an intermediary to store pictures in the database". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, please share it for more people to see.

Tags: Pictures data databases storage users articles intermediaries tools more types components good feasible practical between content suggestions forms arrays articles Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno macOS Xiaomi Shulou Tech Info OPPO Reno NVidia