Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

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

2025-03-28 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report