Firebase Android上的空存储桶

时间:2017-04-12 17:22:46

标签: android firebase firebase-storage

我正在尝试使用firebase存储进行文件上传,因为我从文档中读取我需要一个引用,所以我可以控制事情,所以我这样做了:

FirebaseStorage storage;    
StorageReference storageRef;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_settings);
    storageRef = storage.getReferenceFromUrl("gs://<friendlymanager-3b7a2.appspot.com>");
}

public void editPhoto(View view){
    // Create a reference to "mountains.jpg"
    Intent i = new Intent(Intent.ACTION_PICK);
    i.setType("image/*");
    startActivityForResult(i,GALLERY_IMG);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // Check which request we're responding to
    if (requestCode == GALLERY_IMG  && resultCode == RESULT_OK) {
        Uri uri = data.getData();
        StorageReference filePath = storageRef.child("Photos").child(uri.getLastPathSegment());
        filePath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                Toast.makeText(UserSettings.this, "Upload Done",Toast.LENGTH_LONG).show();
            }
        }).addOnFailureListener(new OnFailureListener() {
           @Override
           public void onFailure(@NonNull Exception e) {

           }
        });
    }
}

它表示这一行:storageRef = storage.getReferenceFromUrl("gs://<friendlymanager-3b7a2.appspot.com>");

有零点预测,发生了什么?,我还需要额外的工作吗?

2 个答案:

答案 0 :(得分:0)

storage为null,因为您尚未实例化它。在使用Firebase Storage执行任何操作之前,您需要创建一个实例:

FirebaseStorage storage = FirebaseStorage.getInstance();

getReferenceFromUrl

之前的onCreate来电之前添加上一行
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_user_settings);
    storage = FirebaseStorage.getInstance();
    storageRef = storage.getReferenceFromUrl("gs://<friendlymanager-3b7a2.appspot.com>");
}

答案 1 :(得分:0)

如果其他人在遵循教程后遇到此问题。 检查FirebaseStorage实例是否实例化如下

let storage = Storage.storage()

而非let storage = Storage()

花了几个小时搞清楚出了什么问题。

:/