Download and parse XML file from Firebase - android

I am creating an Android app which downloads images and XML file from Firebase.
The code for downloading image works fine
FirebaseStorage storage = FirebaseStorage.getInstance();
gsReference = storage.getReferenceFromUrl("gs:...../sample.png");
Glide.with(this)
.using(new FirebaseImageLoader())
.load(gsReference)
.into((ImageView) questionImageSwitcher.getCurrentView());
But I can't retrieve the XML file and read it.

You can get a file from Firebase in that way:
StorageReference firebaseStorageRef = FirebaseStorage.getInstance().getReference(FILE_NAME);
File destinationFile = new File(getFilesDir() + "/" + FILE_NAME);
firebaseStorageRef.getFile(destinationFile)
.addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
// File downloaded successfully, do your stuff here using destinationFile variable
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
// Something went wrong
}
});
and do whatever you want with it.
FILE_NAME is the file's name hosted on Firebase

Related

Downloaded files from firebase storage aren't readable

i succesfully download files from firebase storage and save it but the downloaded file which is pdf type isn't openable
here is the method
#Override
public void download(Book book) {
FirebaseStorage storage = FirebaseStorage.getInstance();
File root = new File(Environment.getExternalStorageDirectory(),"Books");
if(!root.exists()){
root.mkdir();
}
storage.getReference().child(book.getmTitle()+".pdf").getFile(new File(root,book.getmTitle())).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(getActivity(),"Downloaded successfully",Toast.LENGTH_LONG).show();
}
});
}
i am sure that the book.getmTitle() returns the reference name without the extension 'pdf'
Stupid mistake, all what i had to do is to add the extension to the file name

Store Image in Localstorage from Firebase-Storage and Get the Path

I have images stored in firebase storage. Now I want to get the image from storage, download it in local storage and save the path for future use.
What i have tried is:
FirebaseStorage firebaseStorage = FirebaseStorage.getInstance();
StorageReference httpsReference = firebaseStorage.getReferenceFromUrl("gs://****.appspot.com/image_pics/" + userID + ".jpg");
//images are saved with named based on the userid of the user plus an extension of jpg
Now I want to use the httpsReference to download the image in local storage and return the path (in string).
I dont know whether I should use the storage location in firebase db (i.e. the gs// ...) or the url for what I want to do.
Please let me know.
This code will help you to Save the File in Local Storage
FirebaseStorage firebaseStorage = FirebaseStorage.getInstance();
StorageReference httpsReference = firebaseStorage.getReferenceFromUrl("gs://****.appspot.com/image_pics/" + userID + ".jpg"); // IF this is working for you
or
StorageReference myStorage = storageRef.child("file.txt"); // Use this and provide the reference
File rootPath = new File(Environment.getExternalStorageDirectory(), "file_name");
if(!rootPath.exists()) {
rootPath.mkdirs();
}
final File localFile = new File(rootPath,"imageName.txt");
islandRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Log.e("firebase local tem file created created " + localFile.toString());
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
Log.e("firebase ",";local tem file not created created " +exception.toString());
}
});

How to choose specified File to upload to Firebase Storage?

I would like to upload my SQLite database File to Firebase Storage. My problem is that I can only find tutorials where the user has to choose the file manually, but I would like to send a fixed file, so you can't send other data to Firebase storage.
How can I do that?
Thank you.
First get the reference to the firebase storage
// Create a storage reference from our app
StorageReference storageRef = storage.getReference();
Then to upload the file do the following:
Uri file = Uri.fromFile("sqlite_file");
StorageReference filesRef = storageRef.child("files");
uploadTask = filesRef.putFile(file);
// Register observers to listen for when the download is done or if it fails
uploadTask.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
// Handle unsuccessful uploads
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, etc.
// ...
}
});

How to get all files from firebase storage?

I have uploaded some files into Firebase directory and I want to list them and download one by one.There is no API/Documentation fo this.Could you help me ?
As of July 2019, version 18.1.0 of the Cloud Storage SDK now supports listing all objects from a bucket. You simply need to call listAll() in a StorageReference:
StorageReference storageRef = FirebaseStorage.getInstance().getReference();
// Now we get the references of these images
storageRef.listAll().addOnSuccessListener(new OnSuccessListener<ListResult>() {
#Override
public void onSuccess(ListResult result) {
for(StorageReference fileRef : result.getItems()) {
// TODO: Download the file using its reference (fileRef)
}
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(Exception exception) {
// Handle any errors
}
});
If you want to download these files, you can use one of the options shown in the Docs.
Please note that in order to use this method, you must opt-in to version 2 of Security Rules, which can be done by making rules_version = '2'; the first line of your security rules:
rules_version = '2';
service cloud.firestore {
match /databases/{database}/documents {
first , you should get the file download Url to retrieve it, the easiest way is to upload a file and generate the download Url in your database, so after that you just go and retrieve each file from the storage like this :
private void downloadFile() {
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReferenceFromUrl("<your_bucket>");
StorageReference islandRef = storageRef.child("file.txt");
File rootPath = new File(Environment.getExternalStorageDirectory(), "file_name");
if(!rootPath.exists()) {
rootPath.mkdirs();
}
final File localFile = new File(rootPath,"imageName.txt");
islandRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Log.e("firebase ",";local tem file created created " +localFile.toString());
// updateDb(timestamp,localFile.toString(),position);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
Log.e("firebase ",";local tem file not created created " +exception.toString());
}
});
}
I personally use this method, whenever you upload a file, save its download URL in your Firebase Database, if you are uploading multiple files then save it in an array. There is no method for Firebase Storage android to download and upload multiple files in one go.
Whenever you want to download files access your firebase database for those URL's.

Android - Download File from Firebase by means of only URL

I'm developing a simple Android application that downloads a file from Firebase storage.
Is there any way to download a file getting only a link to the file? I found a few methods but they were requiring also the name of file
I don't know the name of downloading file, I need to download the file knowing only its URL.
Just try this :
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference httpsReference = storage.getReferenceFromUrl("YOUR_FIREBASE_STORAGE_URL");
File localFile = File.createTempFile("PREFIX_FILE", "SUFFIX_FILE");
httpsReference.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
// Local temp file has been created
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
// Handle any errors
}
});
Firebase Storage documentation.

Categories

Resources