Reputation: 201
I have a preferences screen where I want a user to select an image. I do this just right. I even manage to display the image after it's selected. But, my question is this. How do I "get" the image? Like getBoolean(), getString(), etc. there is no "getImageUri" or whatever.
Here's my current code for opening the picker and assigning my own ImageView the chosen image
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.activity_settings);
Preference prefereces = findPreference("test");
prefereces.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
public boolean onPreferenceClick(Preference preference) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
int PICK_IMAGE = 1;
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
return true;
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
if (resultCode == RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String filePath = cursor.getString(columnIndex);
cursor.close();
ImageView ok = (ImageView)findViewById(R.id.incognitoView);
ok.setImageURI(selectedImage);
Log.d("OK", "Data Recieved! " + filePath);
}
}
Here's a screenshot to better explain:
Click "select image" brings up the Gallery Picker - actually picking an image puts the image at the center of the view, like so.
Help is appreciated!
Upvotes: 2
Views: 54
Reputation: 3739
If you use a PreferenceClickListener, you have to implement the logic of saving/retrieving the preference yourself.
You can use a SharedPreferences.Editor to edit SharedPreferences:
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
SharedPreferences.Editor editor = sp.edit();
editor.putString("image_uri", imageUri);
editor.apply();
However, I would not recommend to store the image URI inside a SharedPreference, because the URI might not be accessible later. Instead, I would store the image in the file system and put a reference inside a SharedPreference.
You can read a image from URI with a ParcelFileDescriptor:
ParcelFileDescriptor pfd = this.getContentResolver().openFileDescriptor(imageUri, "r");
FileInputStream is = new FileInputStream(pfd.getFileDescriptor());
...
(I would move all code for saving/retrieving preference to an own class.)
public class MySettings {
public static final String PREF_KEY_IMAGE_REF = "pref_image_ref";
private Context mContext;
private SharedPreferences mPreferences;
public MySettings(Context) {
mContext = context;
mPreferences = PreferenceManager.getDefaultSharedPreferences(context);
}
public String getImageRef() {
return mPreferences.getString(PREF_KEY_IMAGE_REF, null);
}
public void setImageRef(String imageRef) {
SharedPreferences.Editor editor = mPreferences.edit();
editor.putString(PREF_KEY_PREF_KEY_IMAGE_REF, imageRef);
editor.apply();
}
}
Upvotes: 2