1
如何在關閉並重新啓動應用程序後還原從庫中挑選的ImageView
。我正在使用SharedPreferences
。要在應用程序關閉後保存URI的狀態,但它不起作用,圖像不會再次設置任何幫助,將不勝感激。應用程序關閉並重新啓動後還原的ImageView
public class test extends AppCompatActivity {
private static int RESULT_LOAD_IMAGE = 1;
ImageButton buttonLoadImage;
private static int RESULT_LOAD_IMG = 1;
String imgDecodableString;
public String getImagePathFetch;
ImageView imgView;
public String keyImage = "myImage";
public SharedPreferences sharedPrefEnter, sharedPrefGet;
Intent galleryIntent;
Uri selectedImage;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity_layout);
imgView = (ImageView) findViewById(R.id.imageView1);
sharedPrefGet = PreferenceManager.getDefaultSharedPreferences(this);
getImagePathFetch = sharedPrefGet.getString(keyImage, "");
if (!getImagePathFetch.equals("")) {
// tToast("OnCreate Path=" + getImagePathFetch);
Uri uri;
uri = Uri.parse(getImagePathFetch);
imgView.setImageURI(uri);
}
buttonLoadImage = (ImageButton) findViewById(R.id.buttonLoadPicture);
buttonLoadImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AddImage(view);
}
});
}
public void AddImage(View view) {
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);
// When an Image is picked
if (requestCode == RESULT_LOAD_IMG && resultCode == RESULT_OK && null != data) {
// Get the Image from data
selectedImage = data.getData();
sharedPrefEnter = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sharedPrefEnter.edit();
editor.putString(keyImage, selectedImage.toString());
editor.commit();
Uri uri = Uri.parse(selectedImage.toString());
imgView.setImageURI(uri);
}
}
}
我已經解決了這個問題,更新我的代碼檢查的運行時權限 – Juancho92