So I've built some code to download a file which works fine and I have set it to download into the applications directory which works. it's stored in the application folder /files/dltest
My issue is with checking programatically wether or not the file exists, I've tried methods one stackoverflow and for some reason I can only get my hard coded path to work.
/sdcard/Android/Data/com.test.alihassan.download/files/dltest/REQS.pdf
Using built in methods to retrieve the path gives me the same path but with /data/data/com.... and this doesn't work
File mydir = context.getFilesDir();
File fileWithinMyDir = new File(mydir, "myfile/path/fileNmae");
if(fileWithinMyDir.exists()){
//exists
}else{
//not exists
}
Update:
//File mydir = this.getFilesDir();
File mydir = this.getExternalFilesDir("/dltest/REQS.pdf");
if (mydir.exists()) {
//exists
} else {
//not exists
}
Related
When downloading a pdf file using DownloadManager, I want to delete it if it already exists in Environment.DIRECTORY_DOWNLOADS.
I check if file exists and delete it using these code:
private boolean fileExists(String fileName) {
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
fileName);
return file.exists();
}
private boolean removeFile(String fileName) {
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
fileName);
return file.delete();
}
removeFile() says that file is deleted, by returning true, and when I look into File Browser only one file is showing up. But when I delete that file it reappers on top of old one. What's wrong?
if i'm not wrong the File file= new File(...) will be your problem.
Because you are using the File class constructor, which is creates a new file to the exact location (File class documentation is here documentation).
Try to give your removeFile(String)method a File type variable, that should do the trick (or use some File 'getting' method from the documentation).
i have tried most of the code i have found on stack over flow read the devlopment documentation but i still fail to create a folder and a file on internal storage in android lollipop i have not tried on lower api but i even tried those internal persmission declaration in manifest.
the below sample of the code i tried do not even work for my situation:
String path = Environment.getDataDirectory().getAbsolutePath().toString() + "/storage/emulated/0/appFolder";
File mFolder = new File(path);
if (!mFolder.exists()) {
mFolder.mkdir();
}
File Directory = new File("/sdcard/myappFolder/");
Directory.mkdirs();
i tried this also below:
File myDir = context.getFilesDir();
// Documents Path
String documents = "documents/data";
File documentsFolder = new File(myDir, documents);
documentsFolder.mkdirs();
String publicC = "documents/public/api.txt" ;
File publicFolder = new File(myDir, publicC);
publicFolder.mkdirs();
and then this as well:
ContextWrapper contextWrapper = new ContextWrapper(
getApplicationContext());
File directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);
myInternalFile = new File(directory, filename);
First, I recommend you read more on what the following terms mean with respect to Android app development:
internal storage
external storage
removable storage
With that as background, let's review what you did:
Environment.getDataDirectory().getAbsolutePath().toString() + "/storage/emulated/0/appFolder";
Never use getDataDirectory(). I have no idea where this code would point to.
File Directory = new File("/sdcard/myappFolder/");
You do not have arbitrary read/write access to removable storage, and removable storage may not be found at that location anyway.
File myDir = context.getFilesDir();
// Documents Path
String documents = "documents/data";
File documentsFolder = new File(myDir, documents);
This code is fine. However, it points to internal storage, and on Android devices, you cannot see internal storage very readily. That too is fine, as developers should be used to the idea that they cannot see everything that their code affects. You might consider writing test cases to confirm that your directory was created.
String publicC = "documents/public/api.txt" ;
File publicFolder = new File(myDir, publicC);
This points to nowhere. Always use some method to derive the base path.
ContextWrapper contextWrapper = new ContextWrapper(
getApplicationContext());
File directory = contextWrapper.getDir(filepath, Context.MODE_PRIVATE);
myInternalFile = new File(directory, filename);
The ContextWrapper is useless. filepath needs to be a simple directory name. You could simplify this as:
File directory = getDir(directoryName, Context.MODE_PRIVATE);
myInternalFile = new File(directory, filename);
Then this code is also fine. It too points to internal storage, and therefore you will not be able to examine it directly. Once again, write test code to confirm that the directory was created as you expect.
I want to check if a text file exists on the SD card. The file name is mytextfile.txt. Below is the code:
FileOutputStream fos = openFileOutput("sdcard/mytextfile.txt", MODE_WORLD_WRITEABLE);
How can I check whether this file exists?
This should do the trick, I've replaced the hard coded SDcard reference to the recommended API call getExternalCacheDir():
File file = new File(getExternalCacheDir(), "mytextfile.txt" );
if (file.exists()) {
//Do action
}
See this file System in android : Working with SDCard’s filesystem in Android
you just check
if(file.exists()){
//
}
*Using this you can check the file is present or not in sdcard *
File file = new File(sdcardpath+ "/" + filename);
if (file.exists())
{
}
You have to create a file, and set it to be the required file, and check if it exists.
String FILENAME="mytextfile.txt";
File fileToCheck = new File(getExternalCacheDirectory(), FILENAME);
if (fileToCheck.exists()) {
FileOutputStream fos = openFileOutput("sdcard/mytextfile.txt", MODE_WORLD_WRITEABLE);
}
Have Fun!
The FileOutputStream constructor will throw a FileNotFound exception if the specified file doesn't exist. See the Oracle documentation for more information.
Also, make sure you have permission to access the SD card.
check IF condition with Boolean type like
File file = new File(path+filename);
if (file.exists() == true)
{
//Do something
}
How can we check the given file is present in Android Filesystem or not without using File inputstream.because I only want to check the existence of the file on the given path.
File myFile = new File("/path/to/file");
if (myFile.isFile()) {
...
}
If you have created a file using openFileOutput method then this works
File mFile = new File(getBaseContext().getFilesDir(),FileName);
if (mFile.exists() )
{
//File is Exits.
}
else
{
//File is not Exits.
}
hope this helps
I have used the Android internal storage to save a file for my application (using openFileOutput) but I would like to delete that file, is it possible and how?
File dir = getFilesDir();
File file = new File(dir, "my_filename");
boolean deleted = file.delete();
I know this is a bit of an oldie, but the docs say to use:
deleteFile("filename");
rather than:
File.delete();
Which if you are already using:
getFilesDir();
kind of makes sense.
You should always delete files that you no longer need. The most straightforward way to delete a file is to have the opened file reference call delete() on itself.
myFile.delete();
If the file is saved on internal storage, you can also ask the Context to locate and delete a file by calling deleteFile():
myContext.deleteFile(fileName);
Note: When the user uninstalls your app, the Android system deletes the following:
All files you saved on internal storage
All files you saved on external storage using getExternalFilesDir().
However, you should manually delete all cached files created with getCacheDir() on a regular basis and also regularly delete other files you no longer need.
Source : http://developer.android.com/training/basics/data-storage/files.html
If you want to delete all files from a folder then use the following function:
private void deleteTempFolder(String dir) {
File myDir = new File(Environment.getExternalStorageDirectory() + "/"+dir);
if (myDir.isDirectory()) {
String[] children = myDir.list();
for (int i = 0; i < children.length; i++) {
new File(myDir, children[i]).delete();
}
}
}
Folder must be present on storage. If not we can check one more codition for it.
if (myDir.exists() && myDir.isDirectory()) {
//write same defination for it.
}
new File(mUri.toString).delete();
void clearMyFiles() {
File[] files = context.getFilesDir().listFiles();
if(files != null)
for(File file : files) {
file.delete();
}
}
Another alternative in Kotlin
val file: File = context.getFileStreamPath("file_name")
val deleted: Boolean = file.delete()
Use delete method of File