dimanche 3 mai 2015

display image on external layout imageview

How I can assign a bitmap of a previously selected image, to a ImageView an external Layout Dialog .. I have this code, which when not assign the layout to strip findViewById returns error, if assign it (which is what I think the problem is) simply nothing happens, not display the image.

Method that selects image gallery

public void selImagen(View v){
Intent intent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI);
int code = SELECT_PICTURE;
startActivityForResult(intent, code);

}

Method that creates the a bitmap image selected and displayed on a imageview

@Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//here is the problem i think, i try assign the layout to the imageview
LayoutInflater li = LayoutInflater.from(this);
View prompt = li.inflate(R.layout.prod_new_modal, null);

Uri selectedImage = data.getData();
InputStream is;
try {
    is = getContentResolver().openInputStream(selectedImage);
    BufferedInputStream bis = new BufferedInputStream(is);
    final Bitmap bitmap = BitmapFactory.decodeStream(bis);
    ImageView imageview = (ImageView)findViewById(R.id.imgView);
    imageview.setImageBitmap(bitmap);

} catch (FileNotFoundException e) {

} catch (IOException e) {
    e.printStackTrace();
}

}

Method of dialog modal

public void newCatModal(View v){

LayoutInflater li = LayoutInflater.from(this);
View prompt = li.inflate(R.layout.prod_new_modal, null);

setSpinner(prompt,0);

AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setView(prompt);
AlertDialog.Builder builder = alertDialogBuilder;

final EditText prodNombre = (EditText) prompt.findViewById(R.id.txtProd);
final EditText prodDesc = (EditText) prompt.findViewById(R.id.txtDesc);
final EditText prodPrecio = (EditText) prompt.findViewById(R.id.txtPrecio);

builder.setCancelable(false);
builder.setPositiveButton(R.string.DialogBotonOk, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        //se sobreescribe abajo
    }
});
builder.setNegativeButton(R.string.DialogBotonCancel, new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
        dialog.cancel();
    }
});

final AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
//Overriding the handler immediately after show is probably a better approach than OnShowListener as described below
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(new View.OnClickListener()
{
    @Override
    public void onClick(View v){
        Boolean wantToCloseDialog = false;

        String nombre = prodNombre.getText().toString();
        String descripcion = prodDesc.getText().toString();
        Integer precio = Integer.parseInt(prodPrecio.getText().toString());

        if (nombre.length()>0){
            if (precio>0){
                guardar(getCatId(),nombre,descripcion,precio,"no");
                try {
                    guardarImg(getImagen());
                } catch (IOException e) {
                    e.printStackTrace();
                }
                listar(true);
                alertDialog.dismiss();
            }else{
                Toast.makeText(getApplicationContext(), R.string.precioVacio, Toast.LENGTH_LONG).show();
            }
        } else {
            Toast.makeText(getApplicationContext(), R.string.NombreVacio, Toast.LENGTH_LONG).show();
        }
    }
});

}

Aucun commentaire:

Enregistrer un commentaire