0

I want to upload Image from drawable to firebase database but I am getting Permission denied error. I have taken permissions also. I have taken Uri of the drawable image and then uploaded it, but it gives permission denied error.

  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();
    }
}

Here are the dependencies that I have taken:

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'

I am getting the following error

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
Diya Bhat
  • 235
  • 3
  • 12

3 Answers3

0

Change your firebase storage rules:

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

Make sure you have done this changes in firebase storage directory, For more rules related to storage refer this link

Rohit Maurya
  • 730
  • 1
  • 9
  • 22
0

Most likely this is because the Database of projects created in the new Firebase Console are readable/writeable only by users that are signed in using Firebase Authentication. Since you're not signing the user in from your code, the database denies you access to the data.

The simplest workaround for the moment is to go into the Database panel in the console for you project, select the Rules tab and replace the contents with these rules:

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

This makes your new database readable and writeable by everyone. Be certain to secure your database again before you go into production, otherwise somebody is likely to start abusing it.

0

I resolved my issue, by changing

compile 'com.google.firebase:firebase-auth:10.0.1'
compile 'com.google.firebase:firebase-storage:10.0.1

to

 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'
Diya Bhat
  • 235
  • 3
  • 12