简体   繁体   English

Firebase云存储中的Android访问文件仍处于离线状态

[英]Android access file in Firebase cloud storage whilst still offline

I need to show images taken by the user in my application. 我需要显示用户在我的应用程序中拍摄的图像。 I storing them in FirebaseFirestore with the following code: 我使用以下代码将它们存储在FirebaseFirestore

StorageReference imagesRef = storage.getReference("images/"+device.ref+"/"+timeStamp+".png");

Bitmap bitmap = BitmapFactory.decodeFile(output.toString());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] image = baos.toByteArray();

UploadTask uploadTask = imagesRef.putBytes(image);
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
    }
});

This works well, and when I am offline and I then go online the files nicely auto upload to Storage . 这很好用,当我离线时又上网时,文件很好地自动上传到Storage

However even when offline I still need to be able to access this image. 但是,即使离线,我仍然需要能够访问此图像。 I am accessing the image normally with the following code: 我正在使用以下代码正常访问图像:

FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageReference = storage.getReference().child("images/"+device+"/"+image.getFilename());

try {
    File localFile = File.createTempFile("images", "jpg");
    localFile.deleteOnExit();

    storageReference.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
            Log.v("Download", "downloaded");
        }
    }).addOnFailureListener(new OnFailureListener() {
        @Override
        public void onFailure(@NonNull Exception exception) {
            // Handle any errors
        }
    });
} catch (IOException e) {
    e.printStackTrace();
}

However if I 'upload' am image whilst offline I can't seem to get it back, is there a reason for this, or perhaps a work around? 但是,如果我在离线状态下“上传”图像,则似乎无法找回图像,这是否有原因,或者可以解决? Suggestions much appreciated. 建议深表感谢。

Cloud Storage for Firebase doesn't have an "offline" mode for dealing with files. Cloud Storage for Firebase没有用于处理文件的“脱机”模式。 It's not like Realtime Database and Firestore. 它不像实时数据库和Firestore。 You can only download a file while online, and you can't download a file that hasn't finished uploading while online. 您只能在线上下载文件,而不能下载在线上尚未完成上传的文件。 If you need to deal with files while offline, you should do that with your own local cache. 如果需要在脱机时处理文件,则应使用自己的本地缓存进行处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM