zoukankan      html  css  js  c++  java
  • Android开发之常用必备工具类图片bitmap转成字符串string与String字符串转换为bitmap图片格式

    作者:程序员小冰,CSDN博客:http://blog.csdn.net/qq_21376985
    QQ986945193 博客园主页:http://www.cnblogs.com/mcxiaobing/

    今天给大家提供一个常用的工具类。

    Android开发之常用必备工具类图片bitmap转成字符串string与String字符串转换为bitmap图片格式
    下面是代码。当然最下面会分享出来源文件。
    下面是代码:

    
    
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.util.Base64;
    
    import java.io.ByteArrayOutputStream;
    
    /**
     * 微博:http://weibo.com/mcxiaobing
     * ============================================================================
     * Copyright (c) 2015-2016 QQ986945193 All rights reserved.
     * ----------------------------------------------------------------------------
     * 类名:Android开发之常用必备工具类图片bitmap转成字符串string与String字符串转换为bitmap图片格式
     * ----------------------------------------------------------------------------
     * 功能描述:Android开发之常用必备工具类图片bitmap转成字符串string与String字符串转换为bitmap图片格式
     * ----------------------------------------------------------------------------
     */
    public class BitmapAndStringUtils {
        /**
         * 图片转成string
         *
         * @param bitmap
         * @return
         */
        public static String convertIconToString(Bitmap bitmap)
        {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();// outputstream
            bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
            byte[] appicon = baos.toByteArray();// 转为byte数组
            return Base64.encodeToString(appicon, Base64.DEFAULT);
    
        }
    
        /**
         * string转成bitmap
         *
         * @param st
         */
        public static Bitmap convertStringToIcon(String st)
        {
            // OutputStream out;
            Bitmap bitmap = null;
            try
            {
                // out = new FileOutputStream("/sdcard/aa.jpg");
                byte[] bitmapArray;
                bitmapArray = Base64.decode(st, Base64.DEFAULT);
                bitmap =
                        BitmapFactory.decodeByteArray(bitmapArray, 0,
                                bitmapArray.length);
                // bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
                return bitmap;
            }
            catch (Exception e)
            {
                return null;
            }
        }
    }

    工具类源代码下载地址:http://download.csdn.net/detail/qq_21376985/9591698

  • 相关阅读:
    图解Go里面的互斥锁mutex了解编程语言核心实现源码
    day04 NTFS安全权限 | 文件共享服务器
    day03 用户与组管理 | 远程管理
    关于VMware的一些资源|IOS|序列号
    day03 批处理
    day02-IP地址详解
    test1
    simulink产生周期矩形波和8421码
    矩阵连乘问题的算法复杂度的计算--卡塔兰数(Catalan数)的数学推导和近似公式
    找出"吸血鬼数"(Java)
  • 原文地址:https://www.cnblogs.com/mcxiaobing/p/5907351.html
Copyright © 2011-2022 走看看