S
S
Semyon Novikov2018-02-15 18:50:21
Java
Semyon Novikov, 2018-02-15 18:50:21

How to download image from firebase storage?

It is not possible to download the image into memory and load it into ImageView. Please help, I'm completely confused. Every time the app crashes , Here is the code:

Image_kyrs = (ImageView) findViewById(R.id.imageView);

         FirebaseStorage storage = FirebaseStorage.getInstance();

        StorageReference storageRef = storage.getReferenceFromUrl("gs://*******.appspot.com");

        StorageReference pathReference = storageRef.child("scoin.png");

        final long ONE_MEGABYTE = 1024 * 1024;
        gsReference.getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
            @Override
            public void onSuccess(byte[] bytes) {
                  Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                  Image_kyrs.setImageBitmap(bmp);

                Toast toast = Toast.makeText(getApplicationContext(),
                        "Картинка скачалась!", Toast.LENGTH_SHORT);
                toast.show();
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                Toast toast = Toast.makeText(getApplicationContext(),
                        "Ошибка!", Toast.LENGTH_SHORT);
                toast.show();
            }
        });
    }

Here are the errors:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageBitmap(android.graphics.Bitmap)' on a null object reference

StorageUtil: error getting token java.util.concurrent.ExecutionException: com.google.firebase.internal.api.FirebaseNoSignedInUserException: Please sign in before trying to get a token.

StorageException has occurred.
User does not have permission to access this object.

Permissions like registered:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
    <uses-permission android:name="android.permission.INTERNET"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

In firebase, reading without authorization is also allowed:
service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
    allow read, write;
    }
  }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ivan0206, 2018-02-16
@semennovikov123

I did it easier. There is a "Picasso" library, in the code you get the url to the image from Firebase and pass it to the library method and it will set this image to the view you need.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question