Augusto Augusto - 2 months ago 22
Android Question

How do I download an image from Firebase Storage on Android?

I'm using Firebase Storage to store images. I can't download these on the Android. I want to download all images of folder but for now I want to download just one, because I don't know how to download all images as a list yet.
I'm trying this

public static final String TAG = "AtividadesFragment";
ImageView imageView;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_atividades,container,false);
Log.i(TAG,"onCreateView()");
imageView = (ImageView) v.findViewById(R.id.image);
return v;
}

@Override
public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);

FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference ref = storage.getReference().child("ImagensExercicios/abdominal_1.bmp");

Glide.with(getActivity()).using(new FirebaseImageLoader()).load(ref).into(imageView);
}


Xml:

<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

</android.support.constraint.ConstraintLayout>


But nothing happens.

enter image description here

Answer Source

I am using the following code to get jpg images from Firebase storage. To get bmp images, I think the difference will be in the parameter of File.createTempFile("Images","jpg") // or bmp. I hope that it will work for you too.

private Bitmap my_image;
StorageReference ref = storage.getReference().child("ImagensExercicios/abdominal_1.bmp");
try {
            final File localFile = File.createTempFile("Images", "bmp");
            storageRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener< FileDownloadTask.TaskSnapshot >() {
                @Override
                public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
                    my_image = BitmapFactory.decodeFile(localFile.getAbsolutePath());
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Toast.makeText(getActivity(), e.getMessage(), Toast.LENGTH_LONG).show();
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

One more note is that I am using this code inside an AsyncTask class to achieve asynchronous task behavior.