how i can pick an image from gallery in android PreferenceScreen menu?
xml code of my PreferenceScreen menu:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<Preference
android:key="select_image"
android:title="select image">
</PreferenceScreen>
i finally found the answer
first set setOnPreferenceClickListener
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;
}
});
than Get it into onActivityResult
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();
Log.d(LOG_TAG, "Data Recieved! " + filePath);
}
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