How do I download a file that is in Storage by my app (Adnroid Studio)

0

I took a look at the documentation provided by Firebase and even then I did not understand how to download a file that is in Storage by my app using Android Studio, can anyone help me?

    
asked by anonymous 13.10.2018 / 17:49

1 answer

1

Save @DevelopD,

1 - First thing to do is select the image you want to upload.

@Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.fb_upload_profile_image:
                Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.INTERNAL_CONTENT_URI);
                startActivityForResult(Intent.createChooser(i, "Selecione uma imagem"), 1000);
                break;
        }
    }

2 - After tried was triggered, we need to get the image selected by the user so we can send it to Firebase Storage

 @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode != Activity.RESULT_CANCELED) {
            if (requestCode == 1000) {

                Uri selectedImage = data.getData();
                File f = new File(String.valueOf(selectedImage));
                Bitmap bitmap = null;
                try {
                    bitmap = MediaStore.Images.Media.getBitmap(getActivity().getContentResolver(), selectedImage);
                } catch (IOException e) {
                    e.printStackTrace();
                }


                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
                byte[] data = baos.toByteArray();

                mStorageRef.child("profile_imgs/".concat(f.getName()))
               .putBytes(data).addOnCompleteListener(new  OnCompleteListener() {
                @Override
                public void onComplete(@NonNull Task task) {
                     if (task.isSuccessful()) {
                         String url = task.getResult()
                        .getDownloadUrl()
                        .toString();
                        /* Essa é a url de onde a imagem fica armazenada. É comum salvá-la no Firebase Database para uso posterior*/
                        Toast.makeText(this, "Foto atualizada com êxito", Toast.LENGTH_LONG)
            .show();
                     } else {
                        Toast.makeText(this, "Não foi possível atualizar a foto de perfil", Toast.LENGTH_LONG).show();
                     }
                 }
               });
            }
        }
    }
    
13.10.2018 / 19:39