標籤:android des blog http java os
MainActivity如下:
package cc.sp;import java.io.ByteArrayInputStream;import java.io.ByteArrayOutputStream;import android.os.Bundle;import android.util.Base64;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;import android.app.Activity;import android.content.Context;import android.content.SharedPreferences;import android.content.SharedPreferences.Editor;import android.graphics.Bitmap;import android.graphics.Bitmap.CompressFormat;import android.graphics.BitmapFactory;/** * Demo描述: * 利用SharedPreferences存取圖片 * * * 參考資料: * 1 http://blog.csdn.net/tangnengwu/article/details/37881087 * 2 http://blog.csdn.net/lfdfhl/article/details/37742775 * 3 http://blog.csdn.net/lfdfhl/article/details/17998469 * 4 http://blog.csdn.net/lfdfhl/article/details/10961459 * Thank you very much * */public class MainActivity extends Activity { private Button mSaveButton; private Button mGetButton; private ImageView mImageView;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);init();}private void init(){mSaveButton=(Button) findViewById(R.id.saveButton);mSaveButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View view) {saveBitmapToSharedPreferences();}});mGetButton=(Button) findViewById(R.id.getButton);mGetButton.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View view) {getBitmapFromSharedPreferences();}});mImageView=(ImageView) findViewById(R.id.imageView);}/** * 將Bitmap轉換成字串儲存至SharedPreferences * * 注意: * 在壓縮圖片至輸出資料流時,不要選擇CompressFormat.JPEG而該是PNG,否則會造成圖片有黑色背景. * 詳見參考資料二 */private void saveBitmapToSharedPreferences(){Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);//第一步:將Bitmap壓縮至位元組數組輸出資料流ByteArrayOutputStreamByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();bitmap.compress(CompressFormat.PNG, 80, byteArrayOutputStream);//第二步:利用Base64將位元組數組輸出資料流中的資料轉換成字串Stringbyte[] byteArray=byteArrayOutputStream.toByteArray();String imageString=new String(Base64.encodeToString(byteArray, Base64.DEFAULT));//第三步:將String保持至SharedPreferencesSharedPreferences sharedPreferences=getSharedPreferences("testSP", Context.MODE_PRIVATE);Editor editor=sharedPreferences.edit();editor.putString("image", imageString);editor.commit();}/** * 從SharedPreferences中取出Bitmap */private void getBitmapFromSharedPreferences(){SharedPreferences sharedPreferences=getSharedPreferences("testSP", Context.MODE_PRIVATE);//第一步:取出字串形式的BitmapString imageString=sharedPreferences.getString("image", "");//第二步:利用Base64將字串轉換為ByteArrayInputStreambyte[] byteArray=Base64.decode(imageString, Base64.DEFAULT);ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(byteArray);//第三步:利用ByteArrayInputStream產生BitmapBitmap bitmap=BitmapFactory.decodeStream(byteArrayInputStream);mImageView.setImageBitmap(bitmap);}}
main.xml如下:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" > <Button android:id="@+id/saveButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="儲存圖片到SharedPreferences" android:layout_centerHorizontal="true" android:layout_marginTop="25dip"/> <Button android:id="@+id/getButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="從SharedPreferences擷取圖片" android:layout_centerHorizontal="true" android:layout_marginTop="80dip"/> <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /></RelativeLayout>