用户无权访问此对象。 Firebase存储android

时间:2023-02-05 22:57:14

I am new to firebase storage. Just so I could learn it, I made a simple app that has a button and an ImageView. When I click on the button, an image (from drawable) gets displayed on the ImageView. I have also written the code for uploading the image on Firebase, but the exception message of onAddFailureListener gives message User does not have permission to access this object. What should I do ?

我是firebase存储的新手。就这样我可以学习它,我做了一个简单的应用程序,它有一个按钮和一个ImageView。当我单击按钮时,ImageView上会显示一个图像(来自drawable)。我还编写了在Firebase上上传图像的代码,但是onAddFailureListener的异常消息给出了消息User没有访问此对象的权限。我该怎么办 ?

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

这是我的firebase存储规则

service firebase.storage {
  match /b/savephoto-a1cc3.appspot.com/o {
    allow read,write;
  }
}

4 个解决方案

#1


25  

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

使用match / {allPaths = **}更新安全规则,以指示所有路径上都允许公共读写访问:

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

Various default rules are provides in the tabs of the Sample Rules section of the documentation.

文档的“示例规则”部分的选项卡中提供了各种默认规则。

#2


8  

Change your storage rules in Firebase console

在Firebase控制台中更改存储规则

用户无权访问此对象。 Firebase存储android

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

#3


1  

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

只需转到存储 - 规则。输入规则如下,将桶名替换为您自己的桶名。您可以转到存储 - 文件找到存储桶名称。

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

它看起来应该是这样的:myapplication-xxxx.appspot.com

That's all you need. You don't need to enable Anonymous authentication.

这就是你所需要的一切。您无需启用匿名身份验证。

用户无权访问此对象。 Firebase存储android

#4


0  

Go to storage -> 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;
    }
  }
}

用户无权访问此对象。 Firebase存储android

#1


25  

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

使用match / {allPaths = **}更新安全规则,以指示所有路径上都允许公共读写访问:

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

Various default rules are provides in the tabs of the Sample Rules section of the documentation.

文档的“示例规则”部分的选项卡中提供了各种默认规则。

#2


8  

Change your storage rules in Firebase console

在Firebase控制台中更改存储规则

用户无权访问此对象。 Firebase存储android

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

#3


1  

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

只需转到存储 - 规则。输入规则如下,将桶名替换为您自己的桶名。您可以转到存储 - 文件找到存储桶名称。

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

它看起来应该是这样的:myapplication-xxxx.appspot.com

That's all you need. You don't need to enable Anonymous authentication.

这就是你所需要的一切。您无需启用匿名身份验证。

用户无权访问此对象。 Firebase存储android

#4


0  

Go to storage -> 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;
    }
  }
}

用户无权访问此对象。 Firebase存储android