Saving Image Picked From Gallery For Future Use
Hey i have been looking for a while now. the following code picks the image from the android gallery and shows it in an imageView. but heres the thing, everytime the app is closed
Solution 1:
The only thing that the user is picking is the path of the picture. So if you save the path to SharedPreferences, then everytime the app is started, you can use your existing code, but just change where you get the path:
StringpicturePath= PreferenceManager.getDefaultSharedPreferences(this).getString("picturePath", "");
if(!picturePath.equals(""))
{
ImageViewimageView= (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
EDIT: This is a complete method you can use in OnCreate:
StringpicturePath= PreferenceManager.getDefaultSharedPreferences(this).getString("picturePath", "");
if(!picturePath.equals(""))
{
ImageViewimageView= (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
else {
selectImage();
}
In select image use your current code to start the picking activity, then in onActivityResult use this:
@OverrideprotectedvoidonActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
UriselectedImage= data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursorcursor= getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
intcolumnIndex= cursor.getColumnIndex(filePathColumn[0]);
StringpicturePath= cursor.getString(columnIndex);
PreferenceManager.getDefaultSharedPreferences(this).edit().putString("picturePath", picturePath).commit();
cursor.close();
ImageViewimageView= (ImageView) findViewById(R.id.imgView);
imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
Post a Comment for "Saving Image Picked From Gallery For Future Use"