我是 Firebase 存储的新手。为了学习它,我制作了一个简单的应用程序,它有一个按钮和一个
ImageView
。当我单击按钮时,图像(from drawable)
会显示在 ImageView 上。我也写了在Firebase上上传图片的代码,但是onAddFailureListener
的异常消息给出消息User does not have permission to access this object. I 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();
}
}
这是我的 firebase 存储规则
service firebase.storage {
match /b/savephoto-a1cc3.appspot.com/o {
allow read,write;
}
}
使用
match /{allPaths=**}
更新您的安全规则,以表明所有路径都允许公共读写访问:
service firebase.storage {
match /b/savephoto-a1cc3.appspot.com/o {
match /{allPaths=**} {
// Allow access by all users
allow read, write;
}
}
}
文档的示例规则部分的选项卡中提供了各种默认规则。
按照这些步骤
转到“存储”
选择“规则”选项卡并按以下方式编辑规则:
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write;
}
}
}
请转到您在 firebase 中的存储/规则文件夹并粘贴以下代码。
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write;
}
}
}
问题出在您的 firebase 存储规则上。改成
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if request.auth != null;
}
}
}
确保
if request.auth != null
只允许经过身份验证的用户写入,但所有人都可以读取:
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read;
allow write: if request.auth != null;
}
}
}
这些解决方案确实没有解决我的问题。
解决了我的问题的是那些行:
在应用程序模块中
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'
以及用于上传和获取下载链接的代码
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();
}
}
service firebase.storage {
match /b/fir-upload-a2fa6.appspot.com/o {
match /{allPaths=**} {
// Allow access by all users
allow read, write;
}
}
}
Firebase 存储错误(已解决) 1. 转到您正在处理的项目下的 firebase 控制台 2. 在 firebase 控制台上的存储和规则下编辑服务 firebase.storage to.
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write;
}
}
}
我添加这个答案希望它能帮助某人,在 Firebase 中:
转到“存储”
选择“规则”选项卡并按以下方式编辑规则:
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
// Allow access by all users
allow read, write: if request.auth != null;
}
}
}
注意:请确保您拥有与上面完全相同的内容,不要将 {bucket} 替换为您的项目名称。
Firebase 存储错误(已解决) 1. 转到您正在处理的项目下的 firebase 控制台 2. 在 firebase 控制台上的存储下和规则下,将服务 firebase.storage 编辑为。
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write;
}
}
}
注意:如果还没有解决此问题,请检查您的 firebase 计划是否已过期。
我刚刚将日期更改为 timestamp.date(2023, 3, 27) 之前的日期 时间戳.日期(2023, 6, 27);
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write: if
request.time < timestamp.date(2023, 6, 27);
}
}
}