我如何在Android PreferenceScreen菜单中从图库中选择图像?
我的PreferenceScreen菜单的xml代码:
<?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>
答案 0 :(得分:11)
我终于找到了答案
首先设置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);
}