Return a pre created dialog in the onActivityResult

2

I'm developing an android app, and I need to allow the user to upload a profile photo. The form for creating the profile is in a dialog, I have the code that loads the profile photo, but loading does not return me to the dialog, it returns to activity. I would like to upload the photo and put it in an imageView that I have in the dialog.

Here is my code:

 ivperfil.setOnClickListener(new View.OnClickListener() {                       
    @Override
    public void onClick(View arg0) {
    // TODO Auto-generated method stub
        Intent i = new Intent(
        Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
        final int ACTIVITY_SELECT_IMAGE = 1234;
        startActivityForResult(i, ACTIVITY_SELECT_IMAGE);
    }
});

And in onActivityResult method I do:

protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    super.onActivityResult(requestCode, resultCode, data); 

    switch(requestCode) { 
    case 1234:
        if(resultCode == RESULT_OK){  
            Uri selectedImage = data.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();


            Bitmap image = BitmapFactory.decodeFile(filePath);
            /* Now you have choosen image in Bitmap format in object "yourSelectedImage". You can use it in way you want! */
            ivperfil.setImageBitmap(image);
            alertD = alertDialogBuilder.create();
            alertD.show();
        }
    }

};

But without ever returning to the dialog, return to activity.

    
asked by anonymous 22.10.2014 / 11:21

1 answer

2

You can follow the following code as example of changing an item's name , and only make the appropriate changes to your case ( obs: item can be a created class can you or your image in your case ):

Creating a custom Dialog:

public class EditNome extends DialogFragment implements
    DialogInterface.OnClickListener {

private ItemListener listener;
private EditText editNome;
private Item item;

@Override
public void onAttach(Activity activity) {

    super.onAttach(activity);
    if (!(activity instanceof ItemListener)) {

        throw new IllegalArgumentException(
                "A activity deve implementar EditNome");
    }
    this.listener = (ItemListener) activity;
}

public void setItem(Item item) {

    this.item = item;
}

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {

    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    builder.setTitle("Editar Nome");
    builder.setPositiveButton("OK", this);
    builder.setNegativeButton("Cancelar", this);
    LayoutInflater inflater = (LayoutInflater) getActivity()
            .getLayoutInflater();
    View view = inflater.inflate(R.layout.dialog, null);
    builder.setView(view);
    this.editNome = (EditText) view.findViewById(R.id.editNome);
    if (item != null) {

        editNome.setText(item.getNome());
    }
    return builder.create();
}

@Override
public void onClick(DialogInterface dialog, int which) {

    if (which == DialogInterface.BUTTON_POSITIVE) {

        String name = editNome.getText().toString();
        if (!TextUtils.isEmpty(name)) {

            listener.changeNome(name);
        }
    }
}


}

After creating an ItemListener interface:

public interface ItemListener {

public void changeNome(String nome);
}

You can call the dialog like this:

EditNome dialog = new EditNome();
dialog.setItem(item);
dialog.show(getFragmentManager(), "Dialog");

Then in your activity you then implement the ItemListener:

@Override
public void changeNome(String nome) {

    //faz algo
}
    
22.10.2014 / 11:40