无法解析符号-位图图像压缩并上传到Firebase

时间:2018-09-16 16:11:05

标签: android firebase firebase-storage

我正在尝试选择一个图像,将其压缩,然后将其上传到Firebase存储。我正在尝试以下代码,但收到错误Cannot resolve symbol 'data2'

这是我的活动:

private void openFileChooserOne() {
    Intent intent = new Intent();
    intent.setType("image/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(intent, PICK_IMAGE_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK &&
        data != null && data.getData() != null) {
        mImageUri = data.getData();
        Picasso.get().load(mImageUri).into(mImageView);
    }
}

private void uploadFile() {
    FirebaseUser user = mAuth.getCurrentUser();
    String userID = user.getUid();
    if (mImageUri != null && mImageMedicalUri != null) {
        StorageReference fileReference = mStorageRef.child(userID).child("image.jpg");
        try {
            Bitmap bmp = MediaStore.Images.Media.getBitmap(getContentResolver(), mImageUri);
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            bmp.compress(Bitmap.CompressFormat.JPEG, 25, baos);
            byte[] data2 = baos.toByteArray();
        } catch (IOException ioEx) {
            ioEx.printStackTrace()
        }
        mUploadTask = fileReference.putBytes(data2) //Getting error here
            .addOnSuccessListener(new OnSuccessListener < UploadTask.TaskSnapshot > () {
                    @Override
                    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
                        //
                    });
            }
    })
}

我是Java的新手,正试图了解问题。非常感谢您的帮助,以了解我在这里做错了什么。

1 个答案:

答案 0 :(得分:1)

使用以下代码行时:

mUploadTask = fileReference.putBytes(data2)

您的data2变量不在声明它的范围内。要解决此问题,您应该将此行移到try-catch块内,或者将data2变量设为全局变量。