2014-02-16 170 views
0

你好,我已經做到了這樣,在我的應用程序中,用戶從點擊一個按鈕指向他們的畫廊的按鈕中選擇一個個人資料圖片。他們選擇了照片,這很好,但一旦我退出應用程序或刪除多任務的個人資料照片已經消失。我怎樣才能保存它?我知道我需要共享首選項,但我不完全確定如何在代碼中使用它。我是初學者。如何在應用程序設置中存儲圖像路徑?

這是我在main.java上的代碼。它展示了我用代碼來選擇從GALLARY butneed照片來保存照片:在SharedPreference

SharedPreferences shre = PreferenceManager.getDefaultSharedPreferences(this); 
Editor edit=shre.edit(); 
edit.putString("profilePic", picturePath); 
edit.commit(); 

import android.media.MediaPlayer; 
import android.net.Uri; 
import android.os.Bundle; 
import android.provider.MediaStore; 
import android.app.Activity; 
import android.content.Intent; 
import android.database.Cursor; 
import android.graphics.BitmapFactory; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 
import android.widget.ImageView; 


public class Main extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

{ 
} 

     Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture); 
     buttonLoadImage.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       Intent i = new Intent(
         Intent.ACTION_PICK, 
         android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 

       startActivityForResult(i, RESULT_LOAD_IMAGE); 
      } 
     });; 


      Button week1 = (Button) findViewById(R.id.week1button); 
      Button week2 = (Button) findViewById(R.id.week2button); 
     //Button Sound 
     final MediaPlayer buttonSound = MediaPlayer.create(Main.this, R.raw.sound1); 


     week1.setOnClickListener(new View.OnClickListener() { 

      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 
       buttonSound.start(); 
       startActivity(new Intent("com.example.timetable.WEEK1")); 
      }}); 

       week2.setOnClickListener(new View.OnClickListener(){ 

        @Override 
        public void onClick(View v) { 
         // TODO Auto-generated method stub 
         buttonSound.start(); 
         startActivity(new Intent("com.example.timetable.WEEK2")); 
        } 
     });  

      }; 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 
     // Inflate the menu; this adds items to the action bar if it is present. 
     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 




private static int RESULT_LOAD_IMAGE = 1; 

    @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 cursor = getContentResolver().query(selectedImage, 
        filePathColumn, null, null, null); 
      cursor.moveToFirst(); 

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      String picturePath = cursor.getString(columnIndex); 
      cursor.close(); 

      ImageView imageView = (ImageView) findViewById(R.id.imageView1); 
      imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 

     } 


    } 
} 
+0

Eclipse只是IDE,而不是在標題中提到平臺。 –

+0

謝謝,那麼你能幫助我嗎? – user3195144

+2

重複的問題 - http://stackoverflow.com/questions/8586242/how-can-i-store-images-using-sharedpreference-in-android – AndyFaizan

回答

2

保存圖像路徑當活動開始時,請選中您SharedPreference。如果存在,請設置圖像。以下代碼可以寫入onCreate()。

SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this); 
String picturePath = prefs.getString("profilePic", ""); 
if(!picturePath.equals("")){ 
     ImageView imageView = (ImageView) findViewById(R.id.imageView1); 
     imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath)); 
} 
+0

我必須爲picturePath創建一個貓頭鷹,它不會讓我使用.getstring它說profilePic,ture。我該怎麼辦? – user3195144

+0

我編輯了代碼,請再試一次。 – Megamind

+0

我得到它的工作,非常感謝。我以爲我的第二部分在我的創始人身上,但沒有明顯的表現。我搬它,現在工作。再次感謝。 – user3195144

相關問題