MainActivity如下:
1 package cc.sp; 2 3 import java.io.ByteArrayInputStream; 4 import java.io.ByteArrayOutputStream; 5 import android.os.Bundle; 6 import android.util.Base64; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button; 10 import android.widget.ImageView; 11 import android.app.Activity; 12 import android.content.Context; 13 import android.content.SharedPreferences; 14 import android.content.SharedPreferences.Editor; 15 import android.graphics.Bitmap; 16 import android.graphics.Bitmap.CompressFormat; 17 import android.graphics.BitmapFactory; 18 /** 19 * Demo描述: 20 * 利用SharedPreferences存取图片 21 * 22 * 23 * 参考资料: 24 * 1 http://blog.csdn.net/tangnengwu/article/details/37881087 25 * 2 http://blog.csdn.net/lfdfhl/article/details/37742775 26 * 3 http://blog.csdn.net/lfdfhl/article/details/17998469 27 * 4 http://blog.csdn.net/lfdfhl/article/details/10961459 28 * Thank you very much 29 * 30 */ 31 public class MainActivity extends Activity { 32 private Button mSaveButton; 33 private Button mGetButton; 34 private ImageView mImageView; 35 @Override 36 protected void onCreate(Bundle savedInstanceState) { 37 super.onCreate(savedInstanceState); 38 setContentView(R.layout.main); 39 init(); 40 } 41 42 private void init(){ 43 mSaveButton=(Button) findViewById(R.id.saveButton); 44 mSaveButton.setOnClickListener(new OnClickListener() { 45 @Override 46 public void onClick(View view) { 47 saveBitmapToSharedPreferences(); 48 } 49 }); 50 mGetButton=(Button) findViewById(R.id.getButton); 51 mGetButton.setOnClickListener(new OnClickListener() { 52 @Override 53 public void onClick(View view) { 54 getBitmapFromSharedPreferences(); 55 } 56 }); 57 mImageView=(ImageView) findViewById(R.id.imageView); 58 } 59 60 61 /** 62 * 将Bitmap转换成字符串保存至SharedPreferences 63 * 64 * 注意: 65 * 在压缩图片至输出流时,不要选择CompressFormat.JPEG而该是PNG,否则会造成图片有黑色背景. 66 * 详见参考资料二 67 */ 68 private void saveBitmapToSharedPreferences(){ 69 Bitmap bitmap=BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher); 70 //第一步:将Bitmap压缩至字节数组输出流ByteArrayOutputStream 71 ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream(); 72 bitmap.compress(CompressFormat.PNG, 80, byteArrayOutputStream); 73 //第二步:利用Base64将字节数组输出流中的数据转换成字符串String 74 byte[] byteArray=byteArrayOutputStream.toByteArray(); 75 String imageString=new String(Base64.encodeToString(byteArray, Base64.DEFAULT)); 76 //第三步:将String保持至SharedPreferences 77 SharedPreferences sharedPreferences=getSharedPreferences("testSP", Context.MODE_PRIVATE); 78 Editor editor=sharedPreferences.edit(); 79 editor.putString("image", imageString); 80 editor.commit(); 81 } 82 83 84 /** 85 * 从SharedPreferences中取出Bitmap 86 */ 87 private void getBitmapFromSharedPreferences(){ 88 SharedPreferences sharedPreferences=getSharedPreferences("testSP", Context.MODE_PRIVATE); 89 //第一步:取出字符串形式的Bitmap 90 String imageString=sharedPreferences.getString("image", ""); 91 //第二步:利用Base64将字符串转换为ByteArrayInputStream 92 byte[] byteArray=Base64.decode(imageString, Base64.DEFAULT); 93 ByteArrayInputStream byteArrayInputStream=new ByteArrayInputStream(byteArray); 94 //第三步:利用ByteArrayInputStream生成Bitmap 95 Bitmap bitmap=BitmapFactory.decodeStream(byteArrayInputStream); 96 mImageView.setImageBitmap(bitmap); 97 } 98 99 }
activity_main.xml如下:
1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 > 6 7 <Button 8 android:id="@+id/saveButton" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:text="保存图片到SharedPreferences" 12 android:layout_centerHorizontal="true" 13 android:layout_marginTop="25dip"/> 14 15 <Button 16 android:id="@+id/getButton" 17 android:layout_width="wrap_content" 18 android:layout_height="wrap_content" 19 android:text="从SharedPreferences获取图片" 20 android:layout_centerHorizontal="true" 21 android:layout_marginTop="80dip"/> 22 23 24 <ImageView 25 android:id="@+id/imageView" 26 android:layout_width="wrap_content" 27 android:layout_height="wrap_content" 28 android:layout_centerInParent="true" 29 /> 30 31 </RelativeLayout>
时间: 2024-10-12 14:13:53