2016-01-28 60 views
0

我有一個應用程序,我允許用戶從Galery中選取圖片,並將其用作PROFILE PICTURE,選擇圖片並在我的應用程序的「ImageView」中設置。退出應用程序後保存ImageView狀態(onSaveInstanceState)

問題是,當應用程序關閉時,您的活動被更改爲圖片desapear,或返回默認圖片再次,我想要保存此圖片狀態爲何時回到活動或關閉並重新打開應用程序照片繼續存在,不需要重新設置。 我是新開發人員,如果你能幫我看下我的代碼,並給出所需的修改,並給我現成的代碼,我將非常感激,因爲我花了好幾天的時間做這件事,但我仍然無法做到。我需要一個現成的代碼,因爲我是開發新手,如果你嘗試解釋一些我不會理解的東西。

這裏是我的代碼,我挑圖片:

public class MainActivity extends Activity { 
private static int RESULT_LOAD_IMG = 1; 
String imgDecodableString; 

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

public void loadImagefromGallery(View view) { 
    // Create intent to Open Image applications like Gallery, Google Photos 
    Intent galleryIntent = new Intent(Intent.ACTION_PICK, 
      android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
    // Start the Intent 
    startActivityForResult(galleryIntent, RESULT_LOAD_IMG); 
} 

@Override 
protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
    super.onActivityResult(requestCode, resultCode, data); 
    try { 
     // When an Image is picked 
     if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK 
       && null != data) { 
      // Get the Image from data 

      Uri selectedImage = data.getData(); 
      String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

      // Get the cursor 
      Cursor cursor = getContentResolver().query(selectedImage, 
        filePathColumn, null, null, null); 
      // Move to first row 
      cursor.moveToFirst(); 

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      imgDecodableString = cursor.getString(columnIndex); 
      cursor.close(); 
      ImageView imgView = (ImageView) findViewById(R.id.imgView); 
      // Set the Image in ImageView after decoding the String 
      imgView.setImageBitmap(BitmapFactory 
        .decodeFile(imgDecodableString)); 

     } else { 
      Toast.makeText(this, "You haven't picked Image", 
        Toast.LENGTH_LONG).show(); 
     } 
    } catch (Exception e) { 
     Toast.makeText(this, "Something went wrong", Toast.LENGTH_LONG) 
       .show(); 
    } 

} 

}

回答