2015-10-15 88 views
0

我的代碼如下將圖片從圖庫中放到我的ImageButton 但是,當我離開應用程序或移動到另一個活動時,圖像不會保存並且第一個背景會再次出現。如何使用sharedpreferences存儲圖像? Android

我需要幫助,我怎麼能救我確定是我的ImageButton背景

我讀到sharedpreferences的形象,但我不知道如何在我的應用程序

使用 -

- 我的課

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 


    //Adding the picture bit 

    imgButton = (ImageButton) findViewById(R.id.AddPic); 
    imgButton.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Intent GaleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
      startActivityForResult(GaleryIntent, RESULT_LOAD_IMAGE); 
     } 
    }); 
} 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

    super.onActivityResult(requestCode, resultCode, data); 
    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { 
     Uri SelectedImage = data.getData(); 
     String[] FilePathColumn = {MediaStore.Images.Media.DATA }; 

     Cursor SelectedCursor = getContentResolver().query(SelectedImage, FilePathColumn, null, null, null); 
     SelectedCursor.moveToFirst(); 

     int columnIndex = SelectedCursor.getColumnIndex(FilePathColumn[0]); 
     String picturePath = SelectedCursor.getString(columnIndex); 
     SelectedCursor.close(); 

     // Drawable d = new BitmapDrawable(getResources(),BitmapFactory.decodeFile(picturePath)); 
     // btnOpenGalery .setImageBitmap(d); 
     imgButton.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 
     Toast.makeText(getApplicationContext(), picturePath, Toast.LENGTH_SHORT).show(); 

    } 

} 

我的XML

<LinearLayout 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" 
    android:gravity="center" 
    android:orientation="vertical" > 

<ImageButton 
    android:id="@+id/AddPic" 
    android:layout_width="100dp" 
    android:layout_height="100dp" 
    android:layout_gravity="center" 
    android:gravity="left" 
    android:onClick="AddPic" 
    android:background="@drawable/ic_launcher" /> 

</LinearLayout> 
+0

刪除了android:背景=「@繪製/ ic_lancher」從你的ImageButton線,它可以幫助你如果以防萬一。 –

+0

可能的重複[如何在Android中共享首選項中保存圖像|在Android中與圖像共享首選項問題](http://stackoverflow.com/questions/18072448/how-to-save-image-in-shared-preference-in-android-shared-preference-issue-in-a) –

回答

0

如果你想使用sharedPreferences,使用下面的代碼:

SharedPreferences sharedPreferences; 

protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.activity_main); 
sharedPreferences = getSharedPreferences("data", context.MODE_PRIVATE); 

    //Adding the picture bit  

imgButton = (ImageButton) findViewById(R.id.AddPic); 
imgButton.setOnClickListener(new OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     Intent GaleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
     startActivityForResult(GaleryIntent, RESULT_LOAD_IMAGE); 
    } 
}); 

if(sharedPreferences!=null) 
String path = sharedPreferences.getString("path", null); 
if(path!=null) 
    imgButton.setImageBitmap(BitmapFactory.decodeFile(path)); 




} 
@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 

super.onActivityResult(requestCode, resultCode, data); 
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) { 
    Uri SelectedImage = data.getData(); 
    String[] FilePathColumn = {MediaStore.Images.Media.DATA }; 

    Cursor SelectedCursor = getContentResolver().query(SelectedImage, FilePathColumn, null, null, null); 
    SelectedCursor.moveToFirst(); 

    int columnIndex = SelectedCursor.getColumnIndex(FilePathColumn[0]); 
    String picturePath = SelectedCursor.getString(columnIndex); 
    SelectedCursor.close(); 
    SharedPreferences.Editor editor = sharedPreferences.edit(); 
    editor.putString("path", picturePath); 
    editor.commit(); 

    // Drawable d = new BitmapDrawable(getResources(),BitmapFactory.decodeFile(picturePath)); 
    // btnOpenGalery .setImageBitmap(d); 
    imgButton.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 
    Toast.makeText(getApplicationContext(), picturePath, Toast.LENGTH_SHORT).show(); 

} 

}

+0

上下文出錯:上下文無法解析爲變量。我做的事? –

+0

刪除它!這裏不需要。 –

+0

沒有工作:/ logcat的一部分: 10-15 06:40:44.795:E/AndroidRuntime(505):引起:java.lang.NullPointerException 10-15 06:40:44.795:E/AndroidRuntime (505):\t at com.example.camera2.MainActivity.onCreate(MainActivity.java:38) –

0

餘did't做這個任務ealier但我猜你可以存儲圖像作爲首Base64編碼字符串。當你想再次獲取該圖像,然後將Base64字符串轉換爲相應的圖像。 您可以按照this鏈接到一個圖像中的Base64字符串轉換,併爲Base64編碼字符串轉換爲圖像看到這個link

相關問題