How do I let a user save an image from gallery to his SharedPreferences?
I am now using this code so the user can select an image from his preferred gallery. How do i save his picture of choice to the SharedPreferences?
if (v.getId() == R.id.btnUploadPicture)
{
Intent uploadPic = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
final int ACTIVITY_SELECT_IMAGE = 1234;
startActivityForResult(uploadPic, ACTIVITY_SELECT_IMAGE);
}
2
Answers
You can save the image path in the shared preferences and later get the image path from the shared preferences.
The image path can be obtained like this.
Correct me if I’m wrong. But it sounds looks like the real issue is that you don’t know how to handle the image when you return from the startActivityForResult.
When you use startActivityForResult, onActivityResult is always ran when it returns from said activity.
You’ll need to override onActivityResult in your class.
I suggesting making
a global variable so you can use it in startActivityForResult. Because you need to surround your code with an if statement, so that it doesn’t get ran when returning from another activity.
Place the following override method in your class.