忽略StorageException

时间:2019-07-07 17:01:28

标签: android firebase firebase-storage

我想检查文件是否存在,并忽略StorageException“找不到文件”

我尝试了onFailureListener,但仍然收到错误消息。

String path = "Pics/" + list.get(i).getId() + ".png";

StorageReference storageRef = storage.getReference();
storageRef.child(path).getDownloadUrl().addOnSuccessListener(MyActivity.this, new OnSuccessListener<Uri>() {
    @Override
    public void onSuccess(Uri uri) {
        Glide.with(MyActivity.this).asBitmap().load(uri).apply(requestOptions).into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {

                imageView.setImageDrawable(new SetRoundedImage().setRoundedImage(resource, getResources()));
            }
        });
    }
}).addOnFailureListener(MyActivity.this, new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception e) {

        //Log.i(TAG, "Ignore " + e.getMessage());

    }
});

错误异常:

  

StorageException:{“错误”:{“代码”:404,“消息”:“未找到。无法获取对象”,“状态”:“ GET_OBJECT”}}               java.io.IOException:{“错误”:{“代码”:404,“消息”:“未找到。无法获取对象”,“状态”:“ GET_OBJECT”}}

1 个答案:

答案 0 :(得分:0)

我认为这种方式无法实现目标。您的目标是将图片从Firebase存储显示到imageview。对?那你为什么不尝试FirebaseUi

下面是一个示例code

首先,您需要使用Glide添加FirebaseUI依赖项

dependencies {
 // FirebaseUI Storage only
 implementation 'com.firebaseui:firebase-ui-storage:4.3.1'

 implementation 'com.github.bumptech.glide:glide:4.8.0'
 // If you're using Kotlin (and therefore, kapt), use kapt instead of annotationProcessor
 annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
}

然后创建appmodule并重建您的android项目,然后直接使用GlideApp

// Reference to an image file in Cloud Storage
StorageReference storageReference = FirebaseStorage.getInstance().getReference();

// ImageView in your Activity
ImageView imageView = findViewById(R.id.imageView);

// Download directly from StorageReference using Glide
// (See MyAppGlideModule for Loader registration)
GlideApp.with(this /* context */)
        .load(storageReference)
        .into(imageView);

此外,您可以使用此方法进行圆形图像裁剪。记得GlideApp可以做Glide可以做的一切

   GlideApp.with(activity)
                        .load(R.drawable.loading)
                        .apply(RequestOptions.circleCropTransform())
                        .into(imageView));