-1

So i have used DownloadManager to download a file from the internet using the following code :

public void DownloadFile()
    {
        Log.d("DownloadFileEntered", "true");

        try {
            String url = "https://firebasestorage.googleapis.com/v0/b/roti-bank-testing.appspot.com/o/SocialGallery%2Fsome.jpg?alt=media&token=d2ddd40f-0c04-441c-82b1-585fd8743b19";
            DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
            request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "somegif.gif");
            DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
            long d = manager.enqueue(request);


        }catch (Exception e)
        {
            Log.d("DownloadFileEntered", "Error."+e.toString());
        }
    }

Now say, i want to delete this file. So to do that, i used the following code :

    File file = new File("file://" + Environment.DIRECTORY_DOWNLOADS + "/somegif.gif");
    boolean d = file.delete();
    Log.d("DownloadFileEntered", "D : "+d);

But apparently, the last Log.d gives the output as false, and the file is also not deleted. So, what is the correct way to delete a file from the external storage?

Also, can we run a function when the download is completed ? if yes then how ?

Femin Dharamshi
  • 157
  • 4
  • 11

1 Answers1

0

Hope this will help you:

File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/somegif.gif");
    boolean d = file.delete();
    Log.d("DownloadFileEntered", "D : "+d);

And don't forget to set this permission in your manifest.xml:

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

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Dinesh Shingadiya
  • 988
  • 1
  • 8
  • 23