首页 > 解决方案 > 将图像上传到 Firebase 时出现“权限被拒绝”错误

问题描述

我想将图像从 drawable 上传到 firebase 数据库,但出现 Permission denied 错误。我也获得了许可。我已经获取了可绘制图像的 Uri 然后上传了它,但它给出了权限被拒绝的错误。

  private Uri mImageUri;
  private StorageReference mStorageRef;
  private DatabaseReference mDatabaseRef;

   private StorageTask mUploadTask;
   @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mImageUri = Uri.parse("android.resource://" + getPackageName() + "/" + 
  R.drawable.mobile);


    mStorageRef = FirebaseStorage.getInstance().getReference("uploads");
    mDatabaseRef = FirebaseDatabase.getInstance().getReference("uploads");
    uploadFile();
}



 private void uploadFile() {
    if (mImageUri != null) {
        StorageReference fileReference = 
          mStorageRef.child(System.currentTimeMillis()
                + "." + getFileExtension(mImageUri));

        mUploadTask = fileReference.putFile(mImageUri)
                .addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        Handler handler = new Handler();
                        handler.postDelayed(new Runnable() {
                            @Override
                            public void run() {
                            }
                        }, 500);

                        Toast.makeText(MainActivity.this, "Upload successful", Toast.LENGTH_LONG).show();
                        Upload upload = new Upload("Image",
                                taskSnapshot.getDownloadUrl().toString());
                        String uploadId = mDatabaseRef.push().getKey();
                        mDatabaseRef.child(uploadId).setValue(upload);
                    }
                })
                .addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception e) {
                        Toast.makeText(MainActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
                    }
                });

    } else {
        Toast.makeText(this, "No file selected", Toast.LENGTH_SHORT).show();
    }
}

以下是我采用的依赖项:

compile 'com.google.firebase:firebase-database:10.0.1'
testCompile 'junit:junit:4.12'
compile 'com.firebase:firebase-client-android:2.3.1'
compile 'com.google.firebase:firebase-auth:10.0.1'
compile 'com.google.firebase:firebase-storage:10.0.1'
compile 'com.android.support:multidex:1.0.0'
implementation 'com.firebase:geofire-android:2.1.1'

我收到以下错误

06-13 10:30:53.416 13173-13173/com.diyabhat.mycart W/DynamiteModule: Failed 
to load module via fast 
routecom.google.android.gms.dynamite.DynamiteModule$zza: V2 version check 
failed
06-13 10:30:53.483 13173-13198/com.diyabhat.mycart W/DynamiteModule: Failed 
to load module via fast 
routecom.google.android.gms.dynamite.DynamiteModule$zza: V2 version check 
failed
06-13 10:30:56.778 12931-12931/? E/Finsky: [1] 
com.google.android.finsky.wear.y.a(3): onConnectionFailed: 
ConnectionResult{statusCode=API_UNAVAILABLE, resolution=null, message=null}
06-13 10:31:14.434 13236-13236/? E/libmdmdetect: Failed to open 
/sys/bus/msm_subsys/devices/subsys0/name: Permission denied
06-13 10:31:14.569 13241-13241/? E/libmdmdetect: Failed to open 
/sys/bus/msm_subsys/devices/subsys0/name: Permission denied
06-13 10:31:15.576 13263-13263/? E/libmdmdetect: Failed to open 
/sys/bus/msm_subsys/devices/subsys0/name: Permission denied

标签: androiddatabasefirebasefirebase-realtime-database

解决方案


更改您的 Firebase 存储规则:

service firebase.storage {
  match /b/{bucket}/o {
    match /{allPaths=**} {
      // Allow access by all users
      allow read, write;
    }
  }
}

确保您已在 firebase 存储目录中进行此更改,有关存储相关的更多规则,请参阅链接


推荐阅读