Reputation: 307
I have created a PreferenceFragment
that loads an xml
file. Within the xml
is an intent
item that starts the image picker. The intent
is as follows:
<PreferenceCategory
android:title="Your Details">
<Preference android:title="Your picture" >
<intent android:action="android.intent.action.PICK"
android:mimeType="image/*"
/>
</Preference>
</PreferenceCategory>
This works fine at displaying the picker and allowing me to make a choice and then return to the settings.
My problem is - how do I find out which image (if any) was chosen?
All the examples I have found so far only seem to demonstrate the intent
being used to view something. Is it actually possible to get a result using this method?
Upvotes: 2
Views: 1138
Reputation: 10943
I didn't create the intent with the xml so I used the event listener to launch the Picker. Here is my code:
public class AccountFragment extends PreferenceFragment implements Preference.OnPreferenceClickListener {
static final int PICK_AVATAR_REQUEST_CODE = 1000;
static final String AVATAR_KEY = "avatar_key";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.account_preferences);
Preference avatar = findPreference(AVATAR_KEY);
avatar.setOnPreferenceClickListener(this);
}
@Override
public boolean onPreferenceClick(Preference preference) {
if(preference.getKey().equals(AVATAR_KEY)){
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, PICK_AVATAR_REQUEST_CODE);
return true;
}
return false;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
// super.onActivityResult(requestCode, resultCode, data);
if(requestCode == PICK_AVATAR_REQUEST_CODE){
/**
* You have to call the getData or getDataString to get the images address
*/
Log.i("CCC", data.getDataString());
}
}
}
But also when you are creating the xml the documentation tells the answer:
Enjoy.
Upvotes: 1