为什么我的firebase存储文件为0字节?

时间:2017-06-28 20:05:20

标签: android firebase firebase-storage

Google Firebase Storage Image (for Reference) 我正在尝试保存从相机意图中拍摄的照片并将其保存到Firebase存储。因为我的目标是Android API 25,所以我必须使用Fileprovider为图片创建URI。 我能够成功打开相机,拍照并将照片上传到我的Firebase存储桶。该文件已上传到Firebase,并且命名正确(在我的代码中指定)。但是,我收到错误“错误加载预览”,文件大小为0字节。当我在我的电脑上下载文件(jpeg)时,windows画廊说“我们无法打开此文件”

还有其他人遇到过这个问题吗?

public class AddPhotoActivity extends AppCompatActivity {
private static final int REQUEST_GALLERY_PICKER = 321;
private static final int CAMERA_REQUEST = 123;

private String mCurrentPhotoPath;
private StorageReference mStorage;

private File createImageFile() throws IOException {
    String timeStamp = new SimpleDateFormat("yyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,
            ".jpg",
            storageDir
    );
    mCurrentPhotoPath = image.getAbsolutePath();
    return image;
}

private void dispatchTakePictureIntent() {
    mStorage = FirebaseStorage.getInstance().getReference();

    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    takePictureIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        // Create the File where the photo should go
        File photoFile = null;
        try {
            photoFile = createImageFile();
        } catch (IOException ex) {
            Toast.makeText(getApplicationContext(), "Error creating file", Toast.LENGTH_SHORT).show();
        }
        Uri photoUri = FileProvider.getUriForFile(this, "co.mymemento.mementocaregiver.fileprovider", photoFile);
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);
        startActivityForResult(takePictureIntent, CAMERA_REQUEST);

        StorageReference filepath = mStorage.child("memento_images").child(photoUri.getLastPathSegment());
        filepath.putFile(photoUri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
            @Override
            public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

            }
        }).addOnFailureListener(new OnFailureListener() {
            @Override
            public void onFailure(@NonNull Exception e) {

            }
        });
    }
}

0 个答案:

没有答案