Persist data from Storage Firebase

Asked

Viewed 176 times

0

Good guys I started working with firebase Torage, I was able to send and receive data from firebase normally using documentation.

However I would like the photo that the user sent and received was maintained even without internet access, someone knows how to do this ?

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();

            }
        });

    }
 }
}

1 answer

1


I see that you are using Picasso to show the image on the screen. The Picasso library can persist the image on the device, but to do this you will need to add one more dependency (Okhttp) and some more code to configure the connection between Picasso and Okhttp. I find it a bit boring, which is why I prefer to use the library Glide. This library persists the image by default, and requires no additional configuration. Replace Picasso with Glide, using the dependencies in 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 method onSucess() will stay:

@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();
}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.