首页 > 代码库 > android 图片base64编码解码

android 图片base64编码解码

android 对图片编码解码demo

package com.example.appdemos;import java.io.ByteArrayOutputStream;import android.app.Activity;import android.graphics.Bitmap;import android.graphics.Bitmap.CompressFormat;import android.graphics.BitmapFactory;import android.os.Bundle;import android.util.Base64;import android.widget.ImageView;public class BaseActivity extends Activity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.base_main);        Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.add);        String string = getBitmapStrBase64(bitmap);                Bitmap bitmaps = stringToBitmap(string);        ImageView img = (ImageView) findViewById(R.id.img);                img.setImageBitmap(bitmaps);    }    /**     * Bitmap 通过Base64 转换为字符串     * @param bitmap     * @return     */    private String getBitmapStrBase64(Bitmap bitmap){        ByteArrayOutputStream bos = new ByteArrayOutputStream();        bitmap.compress(CompressFormat.PNG, 100, bos);        byte[] bytes = bos.toByteArray();        String string = Base64.encodeToString(bytes, Base64.DEFAULT);        return string;    }        /**     * 字符串 转换Bitmap     * @param str     * @return     */    private Bitmap stringToBitmap(String str){        byte[] input = null;        input = Base64.decode(str, Base64.DEFAULT);        Bitmap bitmap = BitmapFactory.decodeByteArray(input, 0, input.length);        return bitmap;    }}

 

android 图片base64编码解码