I have an activity that opens another activity to get a camera gallery pic. The picture comes back to my original activity and rest in an imageView. That's working fine. How do I save the image so when the user comes back later, or kills to app the image is still there. I know I am supposed the use Shared Preferences to get the image path and not save the image itself but I just don't know how do that.
Activity A
private ImageView im1;
private String selectedImagePath;
private static final int SELECT_PICTURE = 1;
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
im1.setImageURI(selectedImageUri);
}}}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
};
((Button)dialogView.findViewById(R.id.button3))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
}});
Activity B
Button send = (Button) findViewById(R.id.send);
send.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent=new Intent();
setResult(RESULT_OK, intent);
Bundle bundle=new Bundle();
bundle.putInt("image",R.id.showImg);
intent.putExtras(bundle);
finish();
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
img.setImageURI(selectedImageUri);
}}}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
Override onPause()
method in Activity
with an image (to understand why onPause
, check life cycle of an Activity diagram here: http://developer.android.com/reference/android/app/Activity.html) like this:
@Override
protected void onPause() {
SharedPrefrences sp = getSharedPreferences("AppSharedPref", 0); // Open SharedPreferences with name AppSharedPref
Editor editor = sp.edit();
editor.putString("ImagePath", selectedImagePath); // Store selectedImagePath with key "ImagePath". This key will be then used to retrieve data.
editor.commit();
super.onPause();
}
It means that whenever this Activity
goes into background, the image path will be saved in SharedPreferences
with name AppSharedPref
- this name can be whatever you like, but you need to use the same one when retrieving data.
Then override onResume()
method in the same Activity
so that you can retrieve image path when Activity
comes to foreground:
@Override
protected void onResume() {
SharedPreferences sp = getSharedPreferences("AppSharedPref", 0);
selectedImagePath = settings.getString("ImagePath", "");
super.onResume();
}
You may also want to play with overriding other methods, like for example onStart()
according to diagram, but this I leave to you.
You could use the "selectedImagePath" in passing intents from one Activity to another.
in Activity A.
Intent intent = new Intent(this , Activity.class); intent.putExtra("imagePath", selectedImagePath );
and to get it in Activity B,
String strImagePath = getIntent().getExtras().getString("imagePath");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With