Deleting file from private storage on android - android

I'm trying to delete a file that I earlier created in my android app.
The problem I'm having is that the file won't go away. Even though everything seems to work.
I've looked at several post here on stackoverflow, but still not solution. The garbage collections was one of the hints I've found.
System.gc();
System.out.println("Exists: "+file.exists());
System.out.println("Read: "+file.canRead());
System.out.println("Write: "+file.canWrite());
System.out.println("Deleting: " + file);
boolean r = file.delete();
System.out.println("Result of deletion: "+r);
System.gc();
And the result in the log
Exists: true
Read: true
Write: true
Deleting: data/data/no.ntnu.kpro.app/files/kprothales/XOMessage/8
Result of deletion: true
Does anyone have any idea as to why it isn't removed?
EDIT:
Lucifer: Yeah, I have set WRITE_EXTERNAL_STORAGE permission in the manifest.
ShineDown: No, it is just a file without an extension. For now it is containing xml, but this is going to change over time, hence why I have not called it .xml. Could this be a problem?
chintan khetiya: I believe this line is allready included in the code above.

check the answer here:
Android: how to delete internal image file
which is basically suggesting to call deleteFile:
if(activity.deleteFile(imageName))
Log.i(TAG, "Image deleted.");

Related

How to programmatically delete a video android and make sure it is removed from device?

Please take a look at my code bellow. Basically, I was able to erase the information contained in my file, but the empty file still exists on my android device. Instead of completely erasing my file, it only removes the information contained in the file.
public void removeCache(Uri uri){
File delete= new File(uri.getPath());
if(delete.exists()){
if(delete.delete()){
Log.d("Deleted", ""+uri);
}
boolean exists= delete.exists();
Log.d("Deleted", "does it exist? " + exists);
}
}
Log Message:
D/Deleted: /storage/emulated/0/secretVideos/NHLPROMO.mp4
D/Deleted: does it exist? false
UPDATE:
So basically, the file does not exist in its direct directory, but download folder in the Android default file viewer stills shows deleted files. This is really annoying and should be removed by google. As of now I do not see any solution to this problem.
File file = new File(audio.getPath());
MediaScannerConnection.scanFile(context,
new String[]{file.toString()},
null, null);
You can try to update the path to see if it still exists or if there is any data

(Ionic) Cordova-file-plugin error when trying to read file

So, I'm currently trying to read an Audio file I just saved on the App's directory (Android) through the cordova file-plugin, but I keep getting the same error code 5, which stands for "ENCODING_ERR".
This is how I create the file and start recording
start() {
this.filename = this.file.externalDataDirectory.replace(/file:\/\//g, '');
this.mediaobject = this.media.create(this.filename + 'audioprofile' + '.3gp');
this.mediaobject.startRecord();
}
This is how I stop recording and save the file
stop() {
this.mediaobject.stopRecord();
this.mediaobject.release();
...
And this is where I'm stuck: right after saving it, I need to have it as a String, so I'm try to read it ( alert(content) should show me that string)
stop() {
this.mediaobject.stopRecord();
this.mediaobject.release();
this.storage.get("uid").then((id) => {
try{
this.file.readAsDataURL(this.filename,'audioprofile'+'.3gp').then((filecontent)=>{
alert(filecontent);
},(err)=>{
alert(err.code);
})
} `
After some research I found out it PROBABLY means I'm not giving the right path for it, but I've tried everything, any combinations of 'filename' and 'filepath' were made, even adding the prefix removed on start().
I want to know if someone managed to read a file with this cordova plugin and if you did, please help me out.
Thanks in advance, this is my first post here \o/ (although I've always used the website, love u guys).
i had the same problem. I solved it giving this path:
this.media.create(this.file.externalDataDirectory + this.nameFile);
I dont know why but this.file.readAsDataURL cant read the file if u save it deleting /file:
Remember change the path in all your methods.
Well i managed to do this with the File-Path Plugin, it resolves the Path for your file in a way the File Plugin understands and is able to reach the file, then you just have to manipulate it the way you want.

Unity - File not writing on Android?

I've been trying to get this to work for the last couple of hours and it's driving me nuts.
I have a method set up to save out data, which works flawlessly on PC, however, on Android it will not write the file. Here's my code:
public void Save()
{
string data = buildJson();
File.WriteAllText(Application.persistentDataPath + "/playerSave.json", data);
Debug.Log("File Saved: " + Application.persistentDataPath + "/playerSave.json");
}
Not sure why it's not working. From what I can tell reading other threads, using the Application.persistantDataPath is the correct thing to do.
Any help?
If WriteAllText failed, an exception would be thrown. Why not try catch the exception and check what the error is?
Solved.
My problem was that the method wasn't even being called correctly.
I had created a singleton gameobject which was holding the script with my save method in it, but I wasn't using a reference to save it, which meant it only worked when I was calling the save method in the same scene the gameobject was created. (Which was the case when I was skipping my menu scene in the Unity editor, but not when I was testing it for real in the app)
On android File.WriteAllText(Application.persistentDataPath + "/playerSave.json", data) will write data on your sd card you need to add permission android.permission.WRITE_EXTERNAL_STORAGE in your manifest file. The default manifest file created by unity do not add this permission.

Deleting a File in Android returns false

I'm trying to delete a file in my Android app. The file was preciously created by the same Android app, and has MODE_WORLD_READABLE permissions.
I'm trying to delete it as follows:
File chosenFile = context.getFileStreamPath("myfile.txt");
boolean fileDeleted = chosenFile.delete();
if (fileDeleted)
Log.d(TAG, "myfile.txt was deleted");
else
Log.d(TAG, "myfile.txt was not deleted");
chosenFile.delete() keeps returning false. Is it because it's still being accessed? If so, is there any way I can force close it?
Thanks.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Did you give this permission.
for this line of code is myfile.txt exists, please check that first, file write permissions are not required for writing in private file system. Its required only when you try to write external storage.
context.getFileStreamPath("myfile.txt");
and to delete the file use
context.deleteFile("myfile.txt");
OK, so I figured out my mistake - the file doesn't exist in the first place. I thought I guarded against that by checking if (chosenFile == null), but I should've checked if (chosenFile.exist() == true) instead.

getExternalFilesDir(null) returns null

Once again, I've come up against a question which has been asked and answered before but in my implementation it is still not working.
I'm calling getExternalFilesDir(null) right at the very start of my main activity's onCreate method. It returns null every time, whether I run it in an AVD or on my phone (Samsung Galaxy Plus).
Yes, I have the <uses-permission android:name="android.permissions.WRITE_EXTERNAL_STORAGE" /> line in my AndroidManifest.xml and yes, I am checking the external storage state before I make the call and it is mounted.
Here are the first three lines inside my onCreate() method. Actually, it's just after the super.onCreate() and setContentView() calls.
String state = Environment.getExternalStorageState();
File extFiles = getExternalFilesDir(null);
File locFiles = getFilesDir();
So, once these three lines have executed, these are the values for the variables:
state == "mounted"
extFiles == null
locFiles == "/data/data/com.mypackage.name/files"
Would anyone have any ideas as to why this might be?
-----EDIT-----
So I've tried another approach; Rather than using getExternalFilesDir(null), I tried using File basePath = new File(Environment.getExternalStorageDirectory(), "myAppName");
This is not ideal and I know that the Android documentation says, and I agree with it, that you should rather use getExternalFilesDir(). Seeing as that's not working for me though I had to try something else. This time the function does return a valid File object so, after the above line, the path of basePath is /mnt/sdcard/myAppName. So far, so good. When I check with DDMS I can see that /mnt/sdcard exists but not /mnt/sdcard/myAppName. This is to be expected. So I call boolean result = basePath.mkdirs();
But this returns false and when I check on the file system I can confirm that the myAppName subfolder has not been created. When I create the folder manually through DDMS and put files in it, I can read those files from my application but I can't write anything in that folder.
Please help! I'm at my wit's end.
If this wasn't a typo when you posted your question, you'll probably hate yourself for this:
<uses-permission android:name="android.permissions.WRITE_EXTERNAL_STORAGE" />
should be
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
This is from Android documentation:
Returns the path of the directory holding application files on external storage.
Returns null if external storage is not currently mounted so it could not ensure
the path exists; you will need to call this method again when it is available.
The other option is you can check if External storage is available:
String state = Environment.getExternalStorageState();
File filesDir;
// Make sure it's available
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
filesDir = getExternalFilesDir(null);
} else {
// Load another directory, probably local memory
filesDir = getFilesDir();
}
My issue was that I opened a FileOutputStream, then before I closed the FileOutputStream, I opened a FileInputStream to see what was already in the file.
I moved opening the FileInputStream to before the FileOutputStream is opened and that fixed my issue.
Delete a line
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
in AndroidManifest.xml.
Switch a xml editor to "Android Manifest Permissions" with "Permission" tab in eclipse, and add a
uses-permission "android.permission.WRITE_EXTERNAL_STORAGE"
with some clicks.
Then try running your application.
It seems eclipse (may depends on a defference of version or state or settings) can ignore some words described by direct xml in AndroidManifest.xml.
Thanks for an advise. You are right, my answer looked like to agree in small talk.

Categories

Resources