android将图片转成字符串,再将字符串转成图片

摘自:http://blog.csdn.net/chinachenjiang/article/details/6939552


安卓字符串转成二进制流再转成图片


找了半天终于可以用了,终于转换成功了,由于要用来加密使用,所以现将要用到的图片转成了字符串,在用字符串转成图片,确切的说是Bitmap类型的,因为是新手,所一说法不是很规范,将其画出来

TestdActivity.Java//这是主程序

public class TestdActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }
}

GameView.java//自定义的GameView类

public class GameView extends View {


private Bitmap [] bitmaps=new Bitmap[9];
public String sdsd=null;
public GameView(Context context) {
super(context);
startBitmap();
}

public GameView(Context context,AttributeSet attributeSet){
super(context,attributeSet);
startBitmap();
}


protected void onDraw(Canvas canvas) {


Bitmap bitmap;
bitmap=stringtoBitmap(bitmaptoString(bitmaps[4]));
Paint nPaint=null;
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(bitmap, 0, 0, nPaint);//将图片显示在画布上
super.onDraw(canvas);

invalidate();
}

    private void startBitmap(){//初始化我的bitmaps[]
bitmaps[0]=BitmapFactory.decodeResource(getResources(), R.drawable.tu1);
bitmaps[1]=BitmapFactory.decodeResource(getResources(), R.drawable.tu2);
bitmaps[2]=BitmapFactory.decodeResource(getResources(), R.drawable.tu3);
bitmaps[3]=BitmapFactory.decodeResource(getResources(), R.drawable.tu4);
bitmaps[4]=BitmapFactory.decodeResource(getResources(), R.drawable.tu5);
bitmaps[5]=null;
bitmaps[6]=BitmapFactory.decodeResource(getResources(), R.drawable.tu6);
bitmaps[7]=BitmapFactory.decodeResource(getResources(), R.drawable.tu7);
bitmaps[8]=BitmapFactory.decodeResource(getResources(), R.drawable.tu8);
    }
    
    
    public Bitmap stringtoBitmap(String string){
    //将字符串转换成Bitmap类型
    Bitmap bitmap=null;
    try {
    byte[]bitmapArray;
    bitmapArray=Base64.decode(string, Base64.DEFAULT);
bitmap=BitmapFactory.decodeByteArray(bitmapArray, 0, bitmapArray.length);
} catch (Exception e) {
e.printStackTrace();
}
    
    return bitmap;
    }
    
    
    
    public String bitmaptoString(Bitmap bitmap){

//将Bitmap转换成字符串
    String string=null;
    ByteArrayOutputStream bStream=new ByteArrayOutputStream();
    bitmap.compress(CompressFormat.PNG,100,bStream);
    byte[]bytes=bStream.toByteArray();
    string=Base64.encodeToString(bytes,Base64.DEFAULT);
    return string;
    }
    
    
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值