`

将图片保存到 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>

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics