我制作了这段代码,将图片上传到firebase,并附带一个子目录"照片"。
我发现了这个错误:
java.lang.IllegalStateException:未使用存储桶名称初始化FirebaseApp。
public class Firebase extends AppCompatActivity {
public TextView text1;
public Button button1;
public Button button2;
public StorageReference mStorage;
public ProgressDialog mProgress;
public static final int GALLERY_INTENT = 2;
public static final String FIREBASE_URL = "https://ivepos.firebaseio.com/weather";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(firebase);
mProgress = new ProgressDialog(this);
mStorage = FirebaseStorage.getInstance().getReference();
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_PICK);
i.setType("image/*");
startActivityForResult(i, GALLERY_INTENT);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == GALLERY_INTENT && resultCode == RESULT_OK){
mProgress.setMessage("Uploading...");
mProgress.show();
Uri uri = data.getData();
StorageReference filepath = mStorage.child("photos").child(uri.getLastPathSegment());
filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(Firebase.this, "Upload done", Toast.LENGTH_SHORT).show();
mProgress.dismiss();
}
});
}
}
}
答案 0 :(得分:2)
您的google-services.json
似乎可能不包含Firebase存储网址。如果您在创建项目后立即下载文件,则可以执行此操作,因为创建存储桶可能需要一些时间。
解决方案是从Firebase Cosnole下载最新的google-services.json
,将其放入项目的应用目录,然后重建应用。
答案 1 :(得分:0)
请检查您的存储桶设置。