Skip to content

Instantly share code, notes, and snippets.

@jirawatee
Last active April 17, 2019 05:48
Show Gist options
  • Save jirawatee/6dd86cb790c1b71183f5aad21d94b82d to your computer and use it in GitHub Desktop.
Save jirawatee/6dd86cb790c1b71183f5aad21d94b82d to your computer and use it in GitHub Desktop.
Firebase Storage - Download in local file
private void downloadInLocalFile() {
File dir = new File(Environment.getExternalStorageDirectory() + "/photos");
final File file = new File(dir, UUID.randomUUID().toString() + ".png");
try {
if (!dir.exists()) {
dir.mkdir();
}
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
final FileDownloadTask fileDownloadTask = imageRef.getFile(file);
Helper.initProgressDialog(this);
Helper.mProgressDialog.setButton(DialogInterface.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
fileDownloadTask.cancel();
}
});
Helper.mProgressDialog.show();
fileDownloadTask.addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Helper.dismissProgressDialog();
mTextView.setText(file.getPath());
mImageView.setImageURI(Uri.fromFile(file));
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Helper.dismissProgressDialog();
mTextView.setText(String.format("Failure: %s", exception.getMessage()));
}
}).addOnProgressListener(new OnProgressListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onProgress(FileDownloadTask.TaskSnapshot taskSnapshot) {
int progress = (int) ((100 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount());
Helper.setProgress(progress);
}
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment