SharedPreferences详解(三)——存取图片
来源:互联网 发布:java输出hello world 编辑:程序博客网 时间:2024/06/04 19:16
MainActivity如下:
main.xml如下:
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>
0 0
- SharedPreferences详解(三)——存取图片
- SharedPreferences详解(二)——存取对象
- android 数据存取——SharedPreferences
- 转 详解C#数据库存取图片三大方式
- SharedPreferences 详解(多进程,存取数组解决方案)
- Android开发——补充SharedPreferences存储(跨Activity存取数据)
- SharedPreferences之数据存取
- 使用Sharedpreferences存取数据
- 使用SharedPreferences存取数据
- SharedPreferences 存取数据
- SharedPreferences存取对象
- SharedPreferences存取bean
- SharedPreferences存取工具SpUtil
- SharedPreferences存取特殊数据
- SharedPreferences基本存取数据
- SharedPreferences实现数据存取
- Android SharedPreferences 存取
- SharedPreferences详解(一)——基础知识
- [leetcode] Path Sum II
- BFS
- POJ3304-Segments
- Linux网络编程-UDP
- 第二章 构架属于自己的WMTS服务,数据下载整合篇1
- SharedPreferences详解(三)——存取图片
- Android学习小Demo(21)ListView的联动选择
- 开始程序猿之路
- 树莓派学习笔记——交叉编译练习之SQLite3安装
- vs2013配置opencv2.4.9
- 6-1. 简单计算器(20)
- 以后就在这里写技术学习笔记啦
- strcmp 遇上 string?
- 使用Socket使用域名连接服务器