• 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;
        }
    }
  • 相关阅读:
    spring boot自动配置
    servlet类与Spring Controller类的关系
    Junit中的setup和teardown方法
    dependency的scope
    mapper的namespace
    mybatis缓存
    Ehcache(05)——缓存的查询
    Ehcache(04)——设置缓存的大小
    Ehcache(03)——Ehcache中储存缓存的方式
    Ehcache(02)——ehcache.xml简介
  • 原文地址:https://www.cnblogs.com/lihaolihao/p/4228510.html
Copyright © 2020-2023  润新知