我终于找到了答案
首先设置 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;
}
});
比把它放进 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);
}