The user does not have permission to access this object. Firebase storage android

I am new to firebase repository. Just so that I can learn it, I created a simple program that has a button and ImageView . When I click on the button, the image (from drawable) displayed in the ImageView. I also wrote code to upload the image to Firebase, but the onAddFailureListener exception onAddFailureListener gives the message The user does not have permission to access this object. What should I do?

 package com.example.asad.save_photo; import android.app.Activity; import android.graphics.Bitmap; import android.os.Bundle; import android.net.Uri; import android.os.Environment; import android.support.annotation.NonNull; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.Toast; import com.google.android.gms.tasks.OnFailureListener; import com.google.android.gms.tasks.OnSuccessListener; import com.google.firebase.storage.FirebaseStorage; import com.google.firebase.storage.StorageMetadata; import com.google.firebase.storage.StorageReference; import com.google.firebase.storage.UploadTask; import java.io.ByteArrayOutputStream; import java.io.File; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); FirebaseStorage storage = FirebaseStorage.getInstance(); StorageReference storageRef = storage.getReferenceFromUrl("gs://savephoto-a1cc3.appspot.com"); final StorageReference mountainsRef = storageRef.child("asad"); Button butt = (Button) findViewById(R.id.button); butt.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { ImageView imageView = (ImageView) findViewById(R.id.image); imageView.setImageResource(R.drawable.back2); //imageView.setImageResource(R.drawable.back2); imageView.setDrawingCacheEnabled(true); imageView.buildDrawingCache(); Bitmap bitmap = imageView.getDrawingCache(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos); final byte[] data = baos.toByteArray(); UploadTask uploadTask = mountainsRef.putBytes(data); uploadTask.addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception exception) { // Handle unsuccessful uploads showToast(exception.getMessage()); } }); uploadTask.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(); showToast("success !!!!!"); } }); } }); } public void showToast(String s) { Toast.makeText(this,s,Toast.LENGTH_LONG).show(); } } 

Here are my firebase storage rules

 service firebase.storage { match /b/savephoto-a1cc3.appspot.com/o { allow read,write; } } 
+14
source share
6 answers

Update your security rules with match /{allPaths=**} to indicate that public read and write access is allowed on all paths:

 service firebase.storage { match /b/savephoto-a1cc3.appspot.com/o { match /{allPaths=**} { // Allow access by all users allow read, write; } } } 

The bookmarks in the Example Rules section contain various default rules.

+35
source

Change retention rules in Firebase console

enter image description here

 service firebase.storage { match /b/{bucket}/o { match /{allPaths=**} { allow read, write: if request.auth != null; } } } 
+11
source

Just go to Storage - Rules. Enter the rule as follows, replacing the bucket name with your own bucket name. You can find the name of the bucket by going to Storage-Files.

It should look something like this: myapplication-xxxx.appspot.com

That is all you need. You do not need to enable authentication if you only do this for testing. If you need authentication, you can add it later.

enter image description here

+3
source

Go to repository โ†’ Rules tab

Add this

 // Anyone can read or write to the bucket, even non-users of your app. // Because it is shared with Google App Engine, this will also make // files uploaded via GAE public. service firebase.storage { match /b/{bucket}/o { match /{allPaths=**} { allow read, write; } } } 

enter image description here

+1
source

These solutions really did not solve my problem.

what solved my problem was the following lines:

in the application module

 implementation 'com.google.firebase:firebase-database:15.0.0' implementation 'com.google.firebase:firebase-storage:15.0.0' implementation 'com.google.firebase:firebase-auth:15.0.0' 

and this code to download and get the download link

 private void uploadImageToFirebase(Bitmap bmp) { try { String file_path = Environment.getExternalStorageDirectory().getAbsolutePath() + "/news"; File dir = new File(file_path); if (!dir.exists()) dir.mkdirs(); File file = new File(dir, "sketchpad" + "_" + ".png"); FileOutputStream fOut = new FileOutputStream(file); bmp.compress(Bitmap.CompressFormat.PNG, 100, fOut); fOut.flush(); fOut.close(); Log.i("SECONDACTIVITY", "ADDED SUCCESSFULLY"); Uri file2 = Uri.fromFile(file); //Now Upload final StorageReference storageRef = FirebaseStorage.getInstance().getReference(); StorageReference riversRef = storageRef.child(file.getName()); UploadTask uploadTask; uploadTask = riversRef.putFile(file2); progressDialog.show(); progressDialog.setCancelable(false); uploadTask.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() { @Override public void onProgress(UploadTask.TaskSnapshot taskSnapshot) { double progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount(); progressDialog.incrementProgressBy((int) progress); } }); uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() { @Override public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) { progressDialog.dismiss(); Uri DownloadLink = taskSnapshot.getDownloadUrl(); String imgUrl = DownloadLink.toString(); FirebaseDatabase.getInstance().getReference().child("image_link").setValue(imgUrl); } }); uploadTask.addOnFailureListener(new OnFailureListener() { @Override public void onFailure(@NonNull Exception e) { progressDialog.dismiss(); } }); } catch (IOException e) { e.printStackTrace(); } } 
0
source
 service firebase.storage { match /b/fir-upload-a2fa6.appspot.com/o { match /{allPaths=**} { // Allow access by all users allow read, write; } } } 
0
source

All Articles