Android Preference Image Picker - как получить результат в DialogPreference
Я хотел бы знать, как получить результат из потока Image Picker в DialogPreference.
Я хотел бы, чтобы DialogPreference
получил вызов после onActivityResult
, чтобы он мог использовать расположение Uri
выбранного изображения для показа в качестве предварительного просмотра изображения пользователю в своем диалоговом окне, прежде чем нажать ok/cancel.
Возможно, мне нужно установить что-то в конце onActivityResult
, а затем вызвать крючок жизненного цикла в DialogPreference
, но я не уверен.
До сих пор логика такова:
ImagePreference.java
public class ImagePreference extends DialogPreference {
View mView;
public ImagePreference(Context context, AttributeSet attrs) {
super(context, attrs);
initWith(context, attrs);
}
private void initWith(Context context, AttributeSet attrs) {
setWidgetLayoutResource(R.layout.pref_image_widget);
setDialogLayoutResource(R.layout.pref_image_dialog);
}
@Override
protected View onCreateDialogView() {
mView = super.onCreateDialogView();
ImageButton button = (ImageButton) mView.findViewById(R.id.add_image);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((SettingsContract.SelectImage)getContext()).fromGallery();
}
});
return mView;
}
SettingsActivity.java
public class SettingsActivity extends AppCompatActivity
implements SettingsContract.SelectImage {
private static final int PICK_IMAGE_REQUEST = 1;
// ...
@Override
public void fromGallery() {
Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
// what to do here??
}
}
Ответы
Ответ 1
Нельзя использовать AlertDialog
в сочетании с ImageView
.
Вы можете установить диалоговое окно, как показано здесь: fooobar.com/info/12210/...
new AlertDialog.Builder(context)
.setTitle("Title")
.setMessage("Here is a preview") //not necessary, you could remove to just show image
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
Чтобы добавить ImageView
, вы можете добавить это до .show()
.setView(imageView);
Вы можете загрузить ImageView
с URI
следующим образом fooobar.com/info/338886/...:
Uri imgUri = Uri.parse(uri);
imageView.setImageURI(null);
imageView.setImageURI(imgUri);