如何将图像上传到 Firebase 存储? [英] How to upload an image to Firebase storage?
问题描述
我正在尝试将一个简单的字节数组上传到 Firebase 存储中,但是我的 onFailureListener
不断被调用并返回给我说上传失败.我希望你们能告诉我我的代码有什么问题.
I'm trying to upload a simple byte array into Firebase storage, but my onFailureListener
keeps getting called and logging back to me saying that the upload failed. I'm hoping you guys can tell me whats wrong with my code.
在顶部我得到了
//Firebase
private Firebase mRef;
private StorageReference storageRef;
然后在onStart()
@Override
protected void onStart() {
super.onStart();
googleApiClient.connect();
//Firebase
mRef = new Firebase("link to firebase account");
FirebaseStorage storage = FirebaseStorage.getInstance();
storageRef = storage.getReferenceFromUrl("link to storage");
}
然后在我的 onActivityResult()
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
addImageImageView.setVisibility(View.GONE);
if (requestCode == Constants.REQUEST_CODE && resultCode == RESULT_OK && data != null) {
//First we gotta make sure to add the images to
ArrayList<Image> imagesFromGallery = data.getParcelableArrayListExtra(Constants.INTENT_EXTRA_IMAGES);
for (int i = 0; i < imagesFromGallery.size(); i++)
{
try {
//try uploading it
InputStream stream = new FileInputStream(new File(imagesFromGallery.get(i).path));
StorageReference imageStorage = storageRef.child("cardImages/" + "testImages");
UploadTask uploadTask = imageStorage.putStream(stream);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Log.d("myStorage","failure :(");
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Log.d("myStorage","success!");
}
});
catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
}
这是我的堆栈跟踪:
11-13 21:27:00.392 31388-996/com.daprlabs.aaron.swipedeck2 E/StorageException: StorageException has occurred.
User does not have permission to access this object.
Code: -13021 HttpResult: 403
11-13 21:27:00.392 31388-996/com.daprlabs.aaron.swipedeck2 E/StorageException: The server has terminated the upload session
java.io.IOException: The server has terminated the upload session
at com.google.firebase.storage.UploadTask.az(Unknown Source)
at com.google.firebase.storage.UploadTask.ay(Unknown Source)
at com.google.firebase.storage.UploadTask.run(Unknown Source)
at com.google.firebase.storage.StorageTask$5.run(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
11-13 21:27:00.406 31388-31388/com.daprlabs.aaron.swipedeck2 D/myStorage: failure :(
推荐答案
您需要登录用户或更改安全规则以允许公共访问.Firebase 存储安全性 的文档中对此进行了说明.
You either need to sign-in the user or change the security rules to allow public access. This is explained in the documentation for Firebase Storage Security.
对于初始开发,您可以在 Firebase 控制台更改规则以允许公开访问:
For initial development, you can change the rules at the Firebase Console to allow public access:
service firebase.storage {
match /b/project-XXXXXXXXXXXXXX.appspot.com/o {
match /{allPaths=**} {
// Provide access to all users
allow read: if true;
allow write: if true;
}
}
}
这篇关于如何将图像上传到 Firebase 存储?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!