将图片保存到 SharedPreferences

Base64ImageActivity.java

package powerise.demo01;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;

import org.apache.commons.codec.binary.Base64;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;

public class Base64ImageActivity extends Activity {
	private Button btn_save = null;
	private ImageView iv_image = null;
	private ImageView iv_image2 = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.base64image);

		btn_save = (Button) findViewById(R.id.btn_save);
		iv_image = (ImageView) findViewById(R.id.iv_image);
		iv_image2 = (ImageView) findViewById(R.id.iv_image2);

		// Field[] fields = R.drawable.class.getDeclaredFields();
		// for (Field field : fields) {
		// System.out.println(field.getName());
		// }
		getImageInfo();
		iv_image.setImageResource(R.drawable.blrise);
		btn_save.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				saveImage();
				getImageInfo();
			}
		});
		saveImage();
	}

	protected void getImageInfo() {
		SharedPreferences mSharedPreferences = getSharedPreferences("image", Context.MODE_PRIVATE);
		String imageBase64 = mSharedPreferences.getString("imageBase64", "");
		byte[] base64Bytes = Base64.decodeBase64(imageBase64.getBytes());
		ByteArrayInputStream bais = new ByteArrayInputStream(base64Bytes);
		iv_image2.setImageDrawable(Drawable.createFromStream(bais, "image"));
	}

	private void saveImage() {
		SharedPreferences mSharedPreferences = getSharedPreferences("image", Context.MODE_PRIVATE);
		Editor mEditor = mSharedPreferences.edit();

		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		((BitmapDrawable) iv_image.getDrawable()).getBitmap().compress(CompressFormat.JPEG, 50, baos);
		String imageBase64 = new String(Base64.encodeBase64(baos.toByteArray()));

		mEditor.putString("imageBase64", imageBase64);
		mEditor.commit();
	}
}

 

 

base64image.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >
	<TextView
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content" 
	    android:text="结果:" />
	<ImageView  
		android:id="@+id/iv_image"
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content"/>
	<Button
		android:id="@+id/btn_save"
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content" 
	    android:text="保存" />
	<ImageView  
		android:id="@+id/iv_image2"
	    android:layout_width="fill_parent" 
	    android:layout_height="wrap_content"/>
</LinearLayout>

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值