Firebase storageReference的getBytes不起作用

时间:2018-10-10 15:14:30

标签: android firebase bitmap firebase-storage android-glide

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

当前,该功能均未进入

 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(位图资源)。

1 个答案:

答案 0 :(得分:0)

您需要等待结果。获取字节方法是异步的,这就是为什么您要添加侦听器,并且在完成异步任务后会设置您的值的原因

相关问题