Update ImageView from a File

5

I am trying to update a ImageView from a File , but I do not get it. Let me explain:

It is a simple app that just uploads and searches for a specific photo on the card and displays it. That makes it great. When I press the button to take a picture, I do it and keep it well, but when I return to the activity it does not show it. Of course, if I leave the app and go back, the new photo does appear. So I think that image remains in memory as such, but I'm not able to update it in the moment. I leave the code to see if they can think of something:

   public class EditoNuevosClientes extends AppCompatActivity {
    int posicion;
    String id;
    ArrayList<Cliente> clientes;
    Cliente cliente=null;
    EditText txtnombre;
    EditText txttlf;
    EditText txtmail;
    EditText txtweb;
    File fichero;
    File foto;
    ImageView fotoNuevoCliente;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_edito_nuevos_clientes);
        id=getIntent().getExtras().getString("id");
        posicion=getIntent().getExtras().getInt("posicion");
        txtnombre=(EditText)findViewById(R.id.txtnombre);
        txttlf=(EditText)findViewById(R.id.txttlf);
        txtmail=(EditText)findViewById(R.id.txtmail);
        txtweb=(EditText)findViewById(R.id.txtweb);
        fotoNuevoCliente=(ImageView)findViewById(R.id.fotoNuevoCliente);



        try{
             fichero=new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Reuniones"+File.separator+id, "otrosClientes.txt");
            ObjectInputStream leo=new ObjectInputStream(new FileInputStream(fichero));
            clientes=(ArrayList) leo.readObject();
            leo.close();
            cliente=clientes.get(posicion);

            txtnombre.setText(cliente.getNombre());
            txtnombre.setEnabled(false);
            txttlf.setText(cliente.getTlf());
            txtmail.setText(cliente.getEmail());
            txtweb.setText(cliente.getWeb());

        }catch (Exception e){
            System.out.println("ERROR DE LECTURA");
            Toast.makeText(this,"error de lectura",Toast.LENGTH_SHORT).show();
        }

try{

     foto=new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Reuniones"+File.separator+id, "fotoContactos"+File.separator+cliente.getNombre()+".jpg");

    if (foto.exists()){
        System.out.println("EXISTEEEEEEEEEEEEEEEE");

        Picasso.with(this).load(foto).into(fotoNuevoCliente);

    }


}catch (Exception e){}


    }

    public void aceptar(View view){

        try{
            cliente.setNombre(txtnombre.getText().toString());
            cliente.setEmail(txtmail.getText().toString());
            cliente.setTlf(txttlf.getText().toString());
            cliente.setWeb(txtweb.getText().toString());
            ObjectOutputStream escribo=new ObjectOutputStream(new FileOutputStream(fichero));
            escribo.writeObject(clientes);
            escribo.close();
        }catch (Exception e){
            System.out.println("FALLO DE ESCRITURA");
        }

        setResult(RESULT_OK);
        finish();
    }

    public void cancelar(View view){
        setResult(RESULT_CANCELED);
        finish();
    }

    public void foto(View view){
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        Uri output = Uri.fromFile(foto);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, output);
        startActivityForResult(intent, 1);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        foto=new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Reuniones"+File.separator+id, "fotoContactos"+File.separator+cliente.getNombre()+".jpg");
            if (foto.exists()){
            System.out.println("EXISTEEEEEEEEEEEEEEEE");
            Picasso.with(this).load(foto).into(fotoNuevoCliente);
        }
    }
}
    
asked by Sergio Cv 29.09.2016 в 17:25
source

2 answers

0

When you start the application again, load the photo with Picasso because it is defined when you enter your Activity , in the onCreate () method:

  foto=new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Reuniones"+File.separator+id, "fotoContactos"+File.separator+cliente.getNombre()+".jpg");

if (foto.exists()){
    System.out.println("EXISTEEEEEEEEEEEEEEEE");

    Picasso.with(this).load(foto).into(fotoNuevoCliente);

}

The problem that you describe, happens since in onActivityResult() you are creating a new file:

  @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        fotoNuevoCliente.setImageResource(android.R.color.transparent);
        foto=new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Reuniones"+File.separator+id, "fotoContactos"+File.separator+cliente.getNombre()+".jpg");
            if (foto.exists()){
            System.out.println("EXISTEEEEEEEEEEEEEEEE");
            //CARGANDO IMAGEN INCORRECTAMENTE!
            Picasso.with(this).load(new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Reuniones"+File.separator+id, "fotoContactos"+File.separator+cliente.getNombre()+".jpg")).into(fotoNuevoCliente);
        }
    }

You must simply upload the image as you do it in onCreate() , since at this point it must be on created disk:

  protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        fotoNuevoCliente.setImageResource(android.R.color.transparent);
        foto=new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "Reuniones"+File.separator+id, "fotoContactos"+File.separator+cliente.getNombre()+".jpg");
            if (foto.exists()){
            System.out.println("EXISTEEEEEEEEEEEEEEEE");
            //CARGANDO IMAGEN CORRECTAMENTE! =)
            Picasso.with(this).load(foto).into(fotoNuevoCliente);
        }
    }
    
answered by 29.09.2016 в 17:53
0

To Picasso you need to send him the Uri, you can not send him the route without converting it more or less I explain:

Define your Uri variable, and the control where it will be displayed:

 Uri fileUriFoto; 
ImageView imgfoto;

You define your method to take the photo and assign the URI more or less as you already have it.

   File fileImage= new File(Environment.getExternalStorageDirectory(),"folder");
  imagesFolder.mkdirs(); 

File image = new File(imagesFolder, "photo.jpg");

//Esta Uri es la misma que vas utilizar para mostrarla.
fileUriFoto = Uri.fromFile(image);

//Creas el intent y le mandas el uri que generaste
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUriFoto);
                                startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);

Define your onActivity for result.

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

        //en caso de que sea una imagen capturada.
        if(requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE)
        {
            if(resultCode == RESULT_OK)
            {

                //Si se capturo la foto correctamente se muestra en el imageView

                Glide.with(this)
                        .load(fileUriFoto.getPath())
                        .diskCacheStrategy(DiskCacheStrategy.NONE)
                        .centerCrop()
                        .crossFade()
                        .into(imgFoto);



            }

       }
}

In this case I use the Glide library to get the image, but it works the same for Picasso, remember to put the URI and the getPath () to get the route and show it.

    
answered by 12.10.2016 в 20:00