SmulianJulian
SmulianJulian

Reputation: 802

Problems getting Image past AlertDialog

I have some code that get's a pic from the camera gallery. It works fine when you use a button from an activity to get pic and put pic on that activity but I cant get it to my Imageview when I try this with a button on AlertDialog. Any help?

This code by itself is fine

    ((Button)dialogView.findViewById(R.id.button3))
            .setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) {
                    Intent intent = new Intent();
                    intent.setType("image/*");
                    intent.setAction(Intent.ACTION_GET_CONTENT);
                    startActivityForResult(Intent.createChooser(intent,"Select Picture"),SELECT_PICTURE);}
                public void onActivityResult(int requestCode, int resultCode, Intent data) {
                    if (resultCode == RESULT_OK) {
                        if (requestCode == SELECT_PICTURE) {
                            Uri selectedImageUri = data.getData();
                            selectedImagePath = getPath(selectedImageUri);
                            System.out.println("Image Path : " + selectedImagePath);
                            im1.setImageURI(selectedImageUri);}}}
                public String getPath(Uri uri) {
                    String[] projection = { MediaStore.Images.Media.DATA };
                    Cursor cursor = managedQuery(uri, projection, null, null, null);
                    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                    cursor.moveToFirst();
                    return cursor.getString(column_index);}});

But when you add this code to AlertDialog it wont function anymore

        lay1.setOnLongClickListener(new OnLongClickListener() {
            public boolean onLongClick(View v) {
                LayoutInflater myLayout = LayoutInflater.from(context);
                final View dialogView = myLayout.inflate(R.layout.alerthelp, null);
                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
                alertDialogBuilder.setView(dialogView);
                final AlertDialog alertDialog = alertDialogBuilder.create();


                ((Button) dialogView.findViewById(R.id.button3))
                        .setOnClickListener(new OnClickListener() {
                            public void onClick(View arg0) {
                                Intent intent = new Intent();
                                intent.setType("image/*");
                                intent.setAction(Intent.ACTION_GET_CONTENT);
                                startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
                            }

                            public void onActivityResult(int requestCode, int resultCode, Intent data) {
                                if (resultCode == RESULT_OK) {
                                    if (requestCode == SELECT_PICTURE) {
                                        Uri selectedImageUri = data.getData();
                                        selectedImagePath = getPath(selectedImageUri);
                                        System.out.println("Image Path : " + selectedImagePath);
                                        im1.setImageURI(selectedImageUri);
                                    }
                                }
                            }

                            public String getPath(Uri uri) {
                                String[] projection = {MediaStore.Images.Media.DATA};
                                Cursor cursor = managedQuery(uri, projection, null, null, null);
                                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                                cursor.moveToFirst();
                                return cursor.getString(column_index);
                            }
                        });
                alertDialog.show();
                return true;
            }
        });

I have tested the code on another activity from a Button and it works fine but when I open alertDialog to do it, I cant get the pic back to my imageview. I can still get to my Gallery and click on a pic but when I try to save it it wont put the pic in the imageView.

Upvotes: 1

Views: 151

Answers (1)

Nachi
Nachi

Reputation: 4248

Try moving the method onActivityResult outside the click listener alongside your other activity methods, like so

public class MyActivity extends Activity {

// other methods and fields

private ImageView im1;

// initialize im1 in your onCreate() method

    public void onActivityResult(int requestCode, int resultCode, Intent data) {
                                if (resultCode == RESULT_OK) {
                                    if (requestCode == SELECT_PICTURE) {
                                        Uri selectedImageUri = data.getData();
                                        selectedImagePath = getPath(selectedImageUri);
                                        System.out.println("Image Path : " + selectedImagePath);
                                        im1.setImageURI(selectedImageUri);
                                    }
                                }
     }
}

Upvotes: 2

Related Questions