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
Related
I m using Firebase as my primary database, some of the local data is storing into sqlite.
I m trying to backup and restore my sqlite db file from local storage to Firebase Storage... I have uploaded the file and now i want to restore it into the same package/databases directory... but couldn't figure it out.
// uploading SQLITE.DB file to FIREBASE STORAGE
#SuppressLint("SdCardPath") String inFileName = "/data/data/com.example.casebook/databases/EVENTS_DB.db";
Uri localDB = Uri.fromFile(new File(inFileName));
final StorageReference DBRef = storageReference.child("users/"+fAuth.getCurrentUser().getUid()+"/EVENTS_DB.csv");
DBRef.putFile(localDB).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(getApplicationContext(), "FILE UPLOADED", Toast.LENGTH_SHORT).show();
}
});
// downloading SQLITE.db file from FIREBASE STORAGE
DBRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
// here i want to get the "EVENTS_DB.db" file to the exact same path "inFileName" for restoring the same DB with help of FIREBASE STORAGE.
}
});
I am making an app to list the uploaded files and then I want to download them with an setOnItemLongClickListener.I get download url and use it to get reference for the download process.When I click on a listview item,it says download is succesful.But I can not see the file on my phone.Actually I have no idea about where it should be.
I saw similar questions but I could not find a solution.I really need help to solve this problem.
Here is my code:
listView.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> arg0, View v,
int index, long arg3) {
try {
storage = FirebaseStorage.getInstance().getReferenceFromUrl(fileFirebase.get(index).toString());
StorageReference island=storage;
final File file = File.createTempFile("images", "jpg");
island.getFile(file).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(getActivity(),"Download is succesful!",Toast.LENGTH_LONG).show();
}
}).addOnProgressListener(new OnProgressListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onProgress(FileDownloadTask.TaskSnapshot taskSnapshot) {
//taskSnapshot.getBytesTransferred();
//taskSnapshot.getTotalByteCount();
}
});}
catch (IOException e) {
e.printStackTrace();
}
File.createTempFile stores the file in the internal cache directory of Android.
You should new File instead and it will be available within the scope of your application directory.
If you are using rooted device or emulator, you can check /data/data/<package-name> to see your downloaded file.
And if not rooted, you can browse through ADB shell to this location. In Android Studio 3 you have a built in File explorer to explore the internal data directory of your App which is /data/data/<package-name>.
You may also consider storing the file if in the external storage. For this, you need to declare permission in the Manifest file.
/* Checks if external storage is available for read and write */
public boolean isExternalStorageWritable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
return true;
}
return false;
}
/* Checks if external storage is available to at least read */
public boolean isExternalStorageReadable() {
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state) ||
Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
return true;
}
return false;
}
Check this link for more information: https://developer.android.com/training/data-storage/files.html
You can get download directory on the device using Environment.getExternalStoragePublicDirectory, see below ...,
For Firebase cloud storage - file upload, download and delete examples, you can see http://www.zoftino.com/firebase-cloud-storage-upload-download-delete-files-android-example#download-file
String DOWNLOAD_DIR = Environment.getExternalStoragePublicDirectory
(Environment.DIRECTORY_DOWNLOADS).getPath();
StorageReference storageRef = firebaseStorage.getReference();
StorageReference downloadRef = storageRef.child(storageFile);
File fileNameOnDevice = new File(DOWNLOAD_DIR+"/"+fileName);
downloadRef.getFile(fileNameOnDevice).addOnSuccessListener(
new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Log.d("File download", "downloaded the file");
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
Log.e("File download", "Failed to download the file");
}
});
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.
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
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.