android - 来自 Firebase storageReference 的 getBytes 不起作用

标签 android firebase bitmap firebase-storage android-glide

我正在创建我的第一个应用程序,我想在其中将图片从 Firebase 存储下载到位图中,然后再更改手机的壁纸。

目前两个函数都没有get

 onSuccess(byte[] bytes) {

或到

 onFailure(@NonNull Exception exception) {

相反,它会直接跳到

return bmp[0]; 

我不明白为什么,我做错了什么。

  • Storage 和 storageRef 不为空。

    public static Bitmap getImageBitmap(String url) {
    
        com.google.firebase.storage.FirebaseStorage storage = com.google.firebase.storage.FirebaseStorage.getInstance();
        StorageReference gsReference = storage.getReferenceFromUrl(url);
    
        if (bmp == null) {
            bmp = new Bitmap[1];
        }
    
         final long ONE_MEGABYTE = 1024 * 1024;
    
         gsReference.getBytes(ONE_MEGABYTE)
        .addOnSuccessListener(new OnSuccessListener<byte[]>(){            
            @Override
            public void onSuccess(byte[] bytes) {
                BitmapFactory.Options options = new 
                BitmapFactory.Options();
                options.inMutable = true;
                bmp[0] = BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception exception) {
                int errorCode = ((StorageException) exception).getErrorCode();
                String errorMessage = exception.getMessage();
                Log.d("TAG", errorMessage + errorCode);
            }
        });
        return bmp[0];
    }
    

在我的尝试中,我尝试通过以下方式使用 Glide:

Glide.with(user.getmContext())
            .asBitmap()
            .load(url)
            .into(new SimpleTarget<Bitmap>() {
                @Override
                public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                    bmp[0] = resource;
                }
            });
    return bmp[0];

但这并没有产生任何变化,onResourceReady(位图资源)永远不会被调用。

最佳答案

您需要等待结果。 Get bytes 方法是异步的,这就是为什么你要添加监听器并且你的值将在异步任务完成后设置

关于android - 来自 Firebase storageReference 的 getBytes 不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52743497/

相关文章:

android - 将一列更新为其他两列的总和

android - 如何在 honeycomb/ics 上打开应用程序小部件选择器?

android - Realm.deleteRealmFile() 已弃用 android?

firebase - 如何将 flutter windows 应用程序与 firebase 集成?

android - 如何在 android 中 blit()?

android - 降低android中位图的DPI分辨率

android - 如何将 DialogFragment 正/负按钮保留在软键盘上方

Firebase Deploy 不更新内容

node.js - Firestore getAll() 方法不接受 FirebaseFirestore.DocumentReference[] 数组

android - bitmap.compress 破坏画质