尝试上传到Firebase时,应用崩溃了

时间:2017-01-27 09:47:40

标签: android firebase firebase-storage

当我尝试上传到Firebase时,我的应用程序崩溃了following error.

我有一个'上传'按钮,用于调用相机。点击后,图片不会上传到Firebase。

我的代码:

private Button mUploadBtn;
private ImageView mImageView;

private static final int CAMERA_REQUEST_CODE = 1;

private StorageReference mStorage;

Uri photoURI;

private ProgressDialog mProgressDialog;

private static final int GALLERY_INTENT = 2;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mStorage = FirebaseStorage.getInstance().getReference();

    mUploadBtn = (Button) findViewById(R.id.upload);

    mImageView = (ImageView) findViewById(R.id.imageView);

    mProgressDialog = new ProgressDialog(this);

    mUploadBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

            startActivityForResult(intent, CAMERA_REQUEST_CODE);
        }
    });

}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {

        mProgressDialog.setMessage("Uplaoding");
        mProgressDialog.show();

        Uri uri = data.getData();



        StorageReference filepath = mStorage.child("Photos").child("file");
        filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                mProgressDialog.dismiss();
                Toast.makeText(MainActivity.this,"Done",Toast.LENGTH_LONG).show();
            }
        });




    }
}

2 个答案:

答案 0 :(得分:0)

您好像没有正确使用相机意图。您显示的代码期望相机应用程序返回的意图通过其getData()方法包含对捕获图像的Uri。这不是它的工作方式。

我建议您按照this tutorial来使用相机。

答案 1 :(得分:0)

重写此方法 并执行以下步骤

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK)
    {
        Uri uri = data.getData();
        StorageReference image_path = storageReference.child("Camera Photos").child(uri.getLastPathSegment());
        image_path.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                Toast.makeText(MainActivity.this, "Image Uploaded", Toast.LENGTH_SHORT).show();
            }
        });
    }

}