SharedPreferences詳解(三)——存取圖片

來源:互聯網
上載者:User

標籤: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>


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.