码迷,mamicode.com
首页 > 移动开发 > 详细

android 图片base64编码解码

时间:2015-01-16 14:40:10      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:

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编码解码

标签:

原文地址:http://www.cnblogs.com/lihaolihao/p/4228510.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!