Android access file in Firebase cloud storage whilst still offline












0















I need to show images taken by the user in my application. I storing them in FirebaseFirestore with the following code:



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.



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.










share|improve this question

























  • When you try to upload image while offline, the image uploads as soon as you get online. To access that image you should first upload it and then get it.

    – Lekr0
    Jan 2 at 16:53











  • But I would like to display the image whilst the user is still offline, is there any way around that?

    – Lewis Smith
    Jan 2 at 16:59











  • You can use local image file (as you said).

    – Lekr0
    Jan 2 at 17:02











  • I know I can, but the reference for that file is in the firebase storage, which until uploaded I can't access

    – Lewis Smith
    Jan 2 at 17:05
















0















I need to show images taken by the user in my application. I storing them in FirebaseFirestore with the following code:



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.



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.










share|improve this question

























  • When you try to upload image while offline, the image uploads as soon as you get online. To access that image you should first upload it and then get it.

    – Lekr0
    Jan 2 at 16:53











  • But I would like to display the image whilst the user is still offline, is there any way around that?

    – Lewis Smith
    Jan 2 at 16:59











  • You can use local image file (as you said).

    – Lekr0
    Jan 2 at 17:02











  • I know I can, but the reference for that file is in the firebase storage, which until uploaded I can't access

    – Lewis Smith
    Jan 2 at 17:05














0












0








0








I need to show images taken by the user in my application. I storing them in FirebaseFirestore with the following code:



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.



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.










share|improve this question
















I need to show images taken by the user in my application. I storing them in FirebaseFirestore with the following code:



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.



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.







java android firebase firebase-storage






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 2 at 17:16









Doug Stevenson

82.6k1099117




82.6k1099117










asked Jan 2 at 16:30









Lewis SmithLewis Smith

172213




172213













  • When you try to upload image while offline, the image uploads as soon as you get online. To access that image you should first upload it and then get it.

    – Lekr0
    Jan 2 at 16:53











  • But I would like to display the image whilst the user is still offline, is there any way around that?

    – Lewis Smith
    Jan 2 at 16:59











  • You can use local image file (as you said).

    – Lekr0
    Jan 2 at 17:02











  • I know I can, but the reference for that file is in the firebase storage, which until uploaded I can't access

    – Lewis Smith
    Jan 2 at 17:05



















  • When you try to upload image while offline, the image uploads as soon as you get online. To access that image you should first upload it and then get it.

    – Lekr0
    Jan 2 at 16:53











  • But I would like to display the image whilst the user is still offline, is there any way around that?

    – Lewis Smith
    Jan 2 at 16:59











  • You can use local image file (as you said).

    – Lekr0
    Jan 2 at 17:02











  • I know I can, but the reference for that file is in the firebase storage, which until uploaded I can't access

    – Lewis Smith
    Jan 2 at 17:05

















When you try to upload image while offline, the image uploads as soon as you get online. To access that image you should first upload it and then get it.

– Lekr0
Jan 2 at 16:53





When you try to upload image while offline, the image uploads as soon as you get online. To access that image you should first upload it and then get it.

– Lekr0
Jan 2 at 16:53













But I would like to display the image whilst the user is still offline, is there any way around that?

– Lewis Smith
Jan 2 at 16:59





But I would like to display the image whilst the user is still offline, is there any way around that?

– Lewis Smith
Jan 2 at 16:59













You can use local image file (as you said).

– Lekr0
Jan 2 at 17:02





You can use local image file (as you said).

– Lekr0
Jan 2 at 17:02













I know I can, but the reference for that file is in the firebase storage, which until uploaded I can't access

– Lewis Smith
Jan 2 at 17:05





I know I can, but the reference for that file is in the firebase storage, which until uploaded I can't access

– Lewis Smith
Jan 2 at 17:05












1 Answer
1






active

oldest

votes


















0














Cloud Storage for Firebase doesn't have an "offline" mode for dealing with files. It's not like Realtime Database and 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.






share|improve this answer
























  • Okay fair point, do you know of any solutions that might be able to provide this, AWS springs to mind but not sure about its offline mode.

    – Lewis Smith
    Jan 2 at 23:26











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009864%2fandroid-access-file-in-firebase-cloud-storage-whilst-still-offline%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









0














Cloud Storage for Firebase doesn't have an "offline" mode for dealing with files. It's not like Realtime Database and 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.






share|improve this answer
























  • Okay fair point, do you know of any solutions that might be able to provide this, AWS springs to mind but not sure about its offline mode.

    – Lewis Smith
    Jan 2 at 23:26
















0














Cloud Storage for Firebase doesn't have an "offline" mode for dealing with files. It's not like Realtime Database and 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.






share|improve this answer
























  • Okay fair point, do you know of any solutions that might be able to provide this, AWS springs to mind but not sure about its offline mode.

    – Lewis Smith
    Jan 2 at 23:26














0












0








0







Cloud Storage for Firebase doesn't have an "offline" mode for dealing with files. It's not like Realtime Database and 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.






share|improve this answer













Cloud Storage for Firebase doesn't have an "offline" mode for dealing with files. It's not like Realtime Database and 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.







share|improve this answer












share|improve this answer



share|improve this answer










answered Jan 2 at 17:18









Doug StevensonDoug Stevenson

82.6k1099117




82.6k1099117













  • Okay fair point, do you know of any solutions that might be able to provide this, AWS springs to mind but not sure about its offline mode.

    – Lewis Smith
    Jan 2 at 23:26



















  • Okay fair point, do you know of any solutions that might be able to provide this, AWS springs to mind but not sure about its offline mode.

    – Lewis Smith
    Jan 2 at 23:26

















Okay fair point, do you know of any solutions that might be able to provide this, AWS springs to mind but not sure about its offline mode.

– Lewis Smith
Jan 2 at 23:26





Okay fair point, do you know of any solutions that might be able to provide this, AWS springs to mind but not sure about its offline mode.

– Lewis Smith
Jan 2 at 23:26




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54009864%2fandroid-access-file-in-firebase-cloud-storage-whilst-still-offline%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

MongoDB - Not Authorized To Execute Command

in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith

Npm cannot find a required file even through it is in the searched directory