Delete file in data directory

4.7k views Asked by At

I have the same problem like in Android, delete files in my data directory?

But it is not working for me.

String sourcefile = getFilesDir().toString() + "/aaa.jpk";
File file = new File(sourcefile);
file.delete();

That should be the easiest way to delete that file but somehow I can't do it. The permissions are added to android manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

so that not the problem. That string is leads me to file :

data/data/com.example.program/files/aaa.jpk

Where is my problem actually? I can't figure that out.

Code for downloading file:

 dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
 Uri resource = Uri.parse("http://www.ecb.int/stats/eurofxref/eurofxref-daily.xml");
 DownloadManager.Request request = new DownloadManager.Request(resource);
 request.setAllowedNetworkTypes(Request.NETWORK_MOBILE | Request.NETWORK_WIFI);
 request.setDestinationInExternalFilesDir(getApplicationContext(), null, "aaa");
 request.setAllowedOverRoaming(false);
 request.setTitle("a.xml");

 dm.enqueue(request);
2

There are 2 answers

6
CommonsWare On BEST ANSWER

Never use concatenation to create a path. Use File file=new File(getFilesDir(), "aaa.jpk"); and see if that helps. Note that your permissions are irrelevant here, as you are working with internal storage, not external storage.

0
IGGYVIP On

Using terminal emulator on rooted phone/tablet its no problem by simply "cd" to folder containing the file and then "su" to request root access and "rm aaa.jpk" it will remove the file

HERE you will find in-depth information on how to use terminal emulator and adb commands