9

I'd like to know how to receive a result from an Image Picker flow in a DialogPreference.

I'd like for the DialogPreference to get called after onActivityResult so it can use the Uri location of the selected image to show as an image preview to the User in their dialog before hitting ok/cancel.

Maybe I need to set something at the end of onActivityResult and then call a lifecycle hook in DialogPreference, but I am not sure.

So far the logic is like this:

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??
    }
}
Aaron Lelevier
  • 19,850
  • 11
  • 76
  • 111
  • Hey, are you giving out the bounty? There is only a half hour left. – Ajay Jan 16 '17 at 20:34
  • @Ajay your answer isn't detailed enough, which is why I didn't award the bounty. I know what you are trying to say, but for any others, especially those newer to Android, they won't get any benefit from your answer without you explaining more. – Aaron Lelevier Jan 17 '17 at 04:26
  • ok, I understand what your saying. I was just worried you forgot about the question, because the reputation is gone for good if it is not rewarded – Ajay Jan 17 '17 at 15:02

1 Answers1

1

Couldn't you use an AlertDialog combined with an ImageView.

You can setup a dialog box as seen here: https://stackoverflow.com/a/2115770/1985387

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();

To add an ImageView, you could add this before the .show()

.setView(imageView);

You could load the ImageView from a URI like this https://stackoverflow.com/a/9080762/1985387:

Uri imgUri = Uri.parse(uri);
imageView.setImageURI(null); 
imageView.setImageURI(imgUri);
Community
  • 1
  • 1
Ajay
  • 437
  • 7
  • 22