In my application some temporary files are created,and its name starts with 'temp'. How to delete these files from sd card on exit .
you can delete files from SDCARD as:
File folder = new File(Environment.getExternalStorageDirectory() + "/tempdirname");
try {
File[] filenamestemp = folder.listFiles();
for(int i=0;i<filenamestemp.length;i++){
if(filenamestemp[i].getAbsolutePath().toString().contains("temp"))
filenamestemp[i].delete();
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
and make sure you have added SDCARD access permission in AndroidManifest.xml :
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Related
I used a lot of examples trying to understand why it doesnt create my file but i didn't and i need some help.
File myFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), "FFFFFFFFF.bin");
try {
myFile.createNewFile();
setOutputStream(new FileOutputStream(myFile));
for (short i = 1; i <= this.array.length; i++) {
this.array[i-1] = new myobject(i);
}
get_objOutputStream().writeObject(this.array);
getOutputStream().close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
the getters/setters are regular, it worked when i wrote it to internal file before i changed it a little so the problem is not an error or related to the array of my objects
thanks for any help
updates:
i got no errors, it just run and then i cant find the file when im looking for it when i plug my phone to my pc after installing and running.
and yes i did wrote the premition, that is not the problem
Did you add permission in manifest file?
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I know how to delete a file, it's:
File file = new File(path);
file.delete();
Can I test that the file is currenlty in use without rooting my device?
For exmple I want to check if the file is open before i can delete it.
I want to be able to catch errors with a try catch sentence.
You can use a try/catch clause.
try {
Files.delete(path);
} catch (NoSuchFileException x) {
System.err.format("%s: no such" + " file or directory%n", path);
} catch (DirectoryNotEmptyException x) {
System.err.format("%s not empty%n", path);
} catch (IOException x) {
// File permission problems are caught here.
System.err.println(x);
}
You can find more information here.
On my code I use:
mp = new MediaPlayer();
String filePath = Environment.getExternalStorageDirectory().getPath() + "/mymusic/asong.mp3";
try {
mp.setDataSource(filePath);
} catch (IOException e) {
e.printStackTrace();
}
try {
mp.prepare();
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mp.start();
And on the emulator the song.mp3 is played normally. But when I test it on my real device, it gives an error (-38, 0). That means it can not find the path of the song. I connect the usb cable, go to my Computer, GT-I8260 and paste the folder "mymusic" (that contains asong.mp3) under "Card" folder (where an empty folder named "LOST.DIR" is also placed). But why doesn't it work? Thanks a lot
it's card but at least
Environment.getExternalStorageDirectory() + "/mymusic/asong.mp3";
is enough.
Make sure it exists because you may not have the folder created before.
File f = new File(Environment.getExternalStorageDirectory() + "/mymusic");
if (!f.exists()) { f.mkdirs(); }
also make sure that it's not mounted while writing, since it may happen that it is not accessable at all.
Also revalidate that you have setupted the manifests permission to read/write the external storage
This one has me stumped (Android 4.3)
I have the right permission:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
... I'm explicitly creating a path to /storage/sdcard0/path/to/data.csv and the FileOutputStream doesn't throw any exceptions.
I tried reading the path via Environment.getExternalStorageDirectory() but that has some "emulated" path.
In either case, I can't see the files on the filesystem ...
What's more confusing is that MEDIA_MOUNTED_READ_ONLYis mounted_ro ... but this isn't the case .
It looks like another app us using /mnt/extSdCard... I tried hard coding that in my app but that doesn't work either. Even the directory is missing. This is very confusing.
I'm calling mkdirs() on this of course and the FileOutputStream is created....
What am I doing wrong?
Try the following code, and see where it fails?
try {
File mPath = new File(android.os.Environment.getExternalStorageDirectory(),"path/to");
if (!mPath.exists()) {
if(!mPath.mkdirs()) {
Log.e(getClass().getName(), "Can't Create Folder: "+mPath.getName());
}
}
File mLogFile = new File(mPath, "data.csv");
FileWriter mFileWriter = null;
if(mPath.canWrite()) {
mFileWriter = new FileWriter(mLogFile, true);
mBufferedWriter = new BufferedWriter(mFileWriter);
}
else {
Log.e(getClass().getName(), "Can't write under Folder: "+mPath.getName());
}
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
I've the sequent trouble. My app should read a list of files and show their name to the user.
When the app is launched for the first time i need to create a specific folder for the app?
then how can i check if it's empty?
Assuming you created the directory in the internal storage, you can get the number of child objects in the directory like this
File dir = context.getDir("somePath", Context.MODE_PRIVATE);
File[] children = dir.listFiles();
if(children.length > 0)
{
// the directory is not empty
}
else
{
// the directory is empty.
}
This is just a quick sample code. The better way would be to exclude the self and parent aliases using a custom FileFilter with dir.listFiles(), as I'm not sure they will always be excluded from the resulting list.
The following code will check if a folder already exists and if not creates one and warns you if error creating one:
// check if appfolder is created and if not through an
// exception
File path = new File("yourDir");
if (!path.exists()) {
if (!path.mkdirs()) {
try {
throw new IOException(
"Application folder can not be created. Please check if your memory is writable and healthy and then try again.");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return false;
}
} else {
Log.i("app", "directory is: " + path.getPath());
}
exists() checks if a path exists or not and path.exists() simply creates one for you.