Reputation: 73
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();
}
Upvotes: 0
Views: 979
Reputation: 6992
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");
}
});
Upvotes: 1
Reputation: 655
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
Upvotes: 1