【问题标题】:Getting 'StorageException' even after setting my rules as public. Please see details即使将我的规则设置为公开后,也会出现“StorageException”。请查看详情
【发布时间】:2016-10-17 12:03:28
【问题描述】:

我在Firebase 上存储了一些图像,当我尝试下载它们时,出现以下错误:E/StorageException: StorageException has occurred. User does not have permission to access this object. Code: -13021 HttpResult: 403。我也在使用谷歌身份验证。

这是我上传图片的方式:

StorageReference storageReference = firebaseStorage.getReferenceFromUrl("gs://appname-e2a32.appspot.com").child("hImage");

UploadTask uploadTask = storageReference.putBytes(data);
uploadTask.addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        Toast.makeText(getBaseContext(), exception.getMessage(), Toast.LENGTH_SHORT).show();
        // Handle unsuccessful uploads
    }
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
        // taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
        Uri downloadUrl = taskSnapshot.getDownloadUrl();
        savingHelpRequest.dismiss();
        Toast.makeText(getBaseContext(), "image uploaded", Toast.LENGTH_SHORT).show();
    }
});

这是我尝试下载它的方式:

FirebaseStorage firebaseStorage = FirebaseStorage.getInstance();

storageReference = firebaseStorage.getReferenceFromUrl("gs://appname-e2a32.appspot.com").child("hImage");

storageReference.getBytes(Long.MAX_VALUE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
    @Override
    public void onSuccess(byte[] bytes) {
        // Use the bytes to display the image
        ByteArrayOutputStream baoStream = new ByteArrayOutputStream();
        bytes = baoStream.toByteArray();
        bmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);

    }
    }).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // Handle any errors
        Toast.makeText(getBaseContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
    }
});

以下是安全规则:

service firebase.storage {
     match /b/appname-e2a32.appspot.com/o {
     match /{allPaths=**} {
     allow read, write;
    }
 }
}

请告诉我如何才能摆脱此错误并成功下载图像?

【问题讨论】:

  • 您是在使用 Firebase 身份验证,还是更改了默认存储安全规则以允许未经身份验证的访问?
  • 请查看已编辑的安全规则问题,是的,我正在使用谷歌身份验证。
  • 你是针对 6.0 编译的吗?
  • @SohailZahid 抱歉,我没听懂你说的。如果您问的是compileSdkVersion,那么它是 23。
  • @HammadNasir 那么在存储图像之前您是否获得了运行时权限?

标签: android firebase firebase-realtime-database firebase-storage


【解决方案1】:

在 Android 6.0 或 API 23 中,我们必须从用户那里获得运行时权限,仅在清单中声明权限是不够的。

  public class SampleActivity extends AppCompatActivity {
        private static final int REQUEST_RUNTIME_PERMISSION = 1;


        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_login);
            checkPremission();
            //SomeTask(); which need permission will become like below in API 23
        }

        void checkPremission() {
            //select which permission you want
            final String permission = Manifest.permission.WRITE_EXTERNAL_STORAGE;
            if (ContextCompat.checkSelfPermission(SampleActivity.this, permission)
                    != PackageManager.PERMISSION_GRANTED) {
                if (ActivityCompat.shouldShowRequestPermissionRationale(SampleActivity.this, permission)) {
                } else {
                    ActivityCompat.requestPermissions(SampleActivity.this, new String[]{permission}, REQUEST_RUNTIME_PERMISSION);
                }
            } else {
                // you have permission go ahead launch service
                SomeTask();
            }
        }

        @Override
        public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
            switch (requestCode) {
                case REQUEST_RUNTIME_PERMISSION:
                    final int numOfRequest = grantResults.length;
                    final boolean isGranted = numOfRequest == 1
                            && PackageManager.PERMISSION_GRANTED == grantResults[numOfRequest - 1];
                    if (isGranted) {
                        // you have permission go ahead
                        SomeTask();
                    } else {
                        // you dont have permission show toast
                    }
                    break;
                default:
                    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
            }
        }


        void SomeTask() {
            FirebaseStorage firebaseStorage = FirebaseStorage.getInstance();
            storageReference = firebaseStorage.getReferenceFromUrl("gs://appname-e2a32.appspot.com");
            storageReference.getBytes(Long.MAX_VALUE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
                @Override
                public void onSuccess(byte[] bytes) {
                    // Use the bytes to display the image
                    ByteArrayOutputStream baoStream = new ByteArrayOutputStream();
                    bytes = baoStream.toByteArray();
                    bmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception exception) {
                    // Handle any errors
                    Toast.makeText(getBaseContext(), exception.getMessage(), Toast.LENGTH_LONG).show();
                }
            });
        }
    }

【讨论】:

  • 你为什么给SomeTask()打了两次电话?
  • 如果您有权限,那么在您授予权限的情况下,无需询问您是否要做其他事情......所以这就是为什么两次
  • @HammadNasir 你的问题解决了吗?
  • 我收到此错误:Cannot resolve symbol 'WRITE_EXTERNAL_STORAGE'.
  • 改成这个android.Manifest.permission.WRITE_EXTERNAL_STORAGE
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2021-06-23
  • 2020-02-27
  • 1970-01-01
  • 1970-01-01
  • 2018-06-30
  • 2023-03-16
  • 2012-08-05
相关资源
最近更新 更多