Keep data coming from Storage Firebase

0

Well guys, I started working with firebase storage, I was able to send and receive firebase data normally using the documentation.

But I would like the photo that the user sent and received was kept even without internet access, does anyone know how to do it?

Fragment that receives the image

mStorageRef = FirebaseStorage.getInstance().getReference();


    //imagens instancia
    imageView = (ImageView) view.findViewById(R.id.imageView4);

    btn_imagem = (ImageButton) view.findViewById(R.id.imageButton);

    btn_imagem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent i = new Intent(Intent.ACTION_PICK);
            i.setType("image/*");
            startActivityForResult(i,GALERIA);

        }
    });

    @Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(requestCode == GALERIA && resultCode == RESULT_OK){
        progressDialog.setMax(100);
        progressDialog.setTitle("Upload de imagem");
        progressDialog.setMessage("Salvando...");
        progressDialog.show();
        Uri uri = data.getData();
        StorageReference filepatch = mStorageRef.child("Fotos").child(uri.getLastPathSegment());
        filepatch.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                progressDialog.dismiss();
                Uri imagemRecebida = taskSnapshot.getDownloadUrl();
                Picasso.with(getActivity()).load(imagemRecebida).fit().centerCrop().into(imageView);
                Toast.makeText(getActivity(), "Upload realizado com sucesso!!!",Toast.LENGTH_SHORT).show();

            }
        });

    }
 }
}
    
asked by anonymous 30.06.2017 / 06:07

1 answer

1

I see that you are using Picasso to display the image on the screen. The Picasso library may persist the image on the device, but to do this you will have to add another dependency (OkHttp) and some code to configure the connection between Picasso and OkHttp. I find this annoying, so I'd rather use the Glide library. This library persists the image by default, and does not require any additional configuration. Replace Picasso with Glide, using the dependencies in the gradle:

repositories {
  mavenCentral()
  google()
}

dependencies {
  implementation 'com.github.bumptech.glide:glide:4.6.1'
  annotationProcessor 'com.github.bumptech.glide:compiler:4.6.1'
}

And your onSucess() method will be:

@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
    progressDialog.dismiss();
    Uri imagemRecebida = taskSnapshot.getDownloadUrl();
    Glide.with(getActivity()).load(imagemRecebida).centerCrop().into(imageView);
    Toast.makeText(getActivity(), "Upload realizado com sucesso!!!",Toast.LENGTH_SHORT).show();
}
    
15.03.2018 / 01:01