Keep image of Firebase once loaded Android

0

I'm trying to create a Activity where you can set a profile image by uploading it to the Storage of Firebase from the gallery through a upload button, and the idea is to remain saved in that activity, but when going on activity, this photo is reset by the one it initially had in gray. I would like to know how I can do to save the changes in the profile photo and that it will not be changed unless another photo is selected.

//pasa la foto al storage
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    //VERIFICA QUE SE HAYA SELECCIONADO UNA FOTO
    if (requestCode == GALLERY_INTENT && resultCode == RESULT_OK) {

        //si todo sale  bien entonces cargamos la barra de espera

        progressDialog.setTitle("Subiendo...");
        progressDialog.setMessage("Subiendo Foto");
        progressDialog.setCancelable(false);//para que al clickear fuera del cuadrado no se salga
        progressDialog.show();

        Uri uri = data.getData();
        //recibe la ruta de la foto o el nombre del archivo
        final StorageReference filePath = miStorageReferenceImagen.child("fotos").child(uri.getLastPathSegment());

        //sube la foto a la carpeta en storage que acabamos de crear
        filePath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

                progressDialog.dismiss();//finaliza la barra de carga

                //OBTIENE EL URI DE LA FOTO
                filePath.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
                    @Override
                    public void onSuccess(Uri uri) {
                        //la librería glide se añade al gradle app y al project, para guardar imágenes en caché etc
                        Glide.with(getBaseContext())
                                .load(uri).into(civPerfil1);//con fit center y .centercrop se acomoda la imagen pero solo sirve para los images views
                        Toast.makeText(getBaseContext(), uri.toString(), Toast.LENGTH_SHORT).show();

                    }
                });
            }
        });

    }
}

//metodo subir foto
public void subirFoto(View v) {

    Intent intent = new Intent(Intent.ACTION_PICK);//escoger imagen de la galería
    intent.setType("image/*");//abarca todos los formatos de images
    startActivityForResult(intent, GALLERY_INTENT);

}
    
asked by Esteban Lotero 27.05.2018 в 17:52
source

1 answer

0

It occurs to me that when you have the URL of the image provided by Firebase Storage you save it using SharedPreference (with a unique key) and simply where you want to show it, retrieve it from the SharedPreference and show it as in your ImageView with Glide . If the user has the option to change the profile image when you want you will have to repeat the process and save the new link in SharedPreference with the same key so that you do not save data that you will not occupy in the SharedPreference.

    
answered by 29.05.2018 в 20:32