将图像上传到Firebase时,我收到“权限被拒绝”错误

时间:2018-06-13 05:16:47

标签: android database firebase firebase-realtime-database

我想将图像从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

3 个答案:

答案 0 :(得分:1)

这很可能是因为在新Firebase控制台中创建的项目数据库只能由使用Firebase身份验证登录的用户读取/写入。由于您未从代码中签署用户,因此数据库拒绝您访问数据。

目前最简单的解决方法是进入项目控制台的“数据库”面板,选择“规则”选项卡并使用以下规则替换内容:

{
  "rules": {
    ".read": true,
    ".write": true
  }
}

这使得您的新数据库可供所有人阅读和编写。确保在投入生产之前再次保护您的数据库,否则有人可能会开始滥用它。

答案 1 :(得分:0)

更改您的firebase存储规则:

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

确保您在firebase存储目录中完成了此更改,有关存储的更多规则,请参阅https://localhost:8983链接

答案 2 :(得分:0)

我通过更改

解决了我的问题
compile 'com.google.firebase:firebase-auth:10.0.1'
compile 'com.google.firebase:firebase-storage:10.0.1

 compile 'com.google.firebase:firebase-database:11.0.4'
 compile 'com.google.firebase:firebase-storage:11.0.4'
  compile 'com.google.firebase:firebase-auth:11.0.4'