Search code examples
javaandroidapache-commonsandroid-download-manager

Can't move file after DownloadManager completes download


in my Android app I am using the system Download Manager to download a photo, once recieved I process it and then I need to move it to another location. if I just copy it it works fine, if I try to move it/delete it I get an exception about it can't be moved.

Failed to delete original file '/data/user/0/com.android.providers.downloads/cache/a3133p2930-9.jpg' after copy to '/data/data/com.XXXXX.testharness/app_appdata/images/one/two/three/6/a3133p2930.jpg'

also tried:

1) remove it from download manager before trying to move it 2) set the destination directly to the target dir, but got an error I can only use public external folders. 3) tried using JAVA renameTo and Apache commons moveFile

this is a code snippet that show the relevant parts of what I am doing.

any ideas?

appreciated

@Override
public void onReceive(Context context, Intent intent) {

    String action = intent.getAction();
    if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {


    String uriString = c.getString(
                        c.getColumnIndex(
                                DownloadManager.COLUMN_LOCAL_URI));


    String uri2String = c.getString(
                            c.getColumnIndex(
                                    DownloadManager.COLUMN_URI));

    String localFileString = c.getString(
                                c.getColumnIndex(
                                        DownloadManager.COLUMN_LOCAL_FILENAME));

    File from = new File(localFileString);
    File to = new File(appDataDir, localString);
    File to2 = new File (appDataDir, localString + "/" + getFileNameFromUrl(uri2String));


    if (!to.exists()) {
        if (to.mkdirs()) {
            Log.d(LCAT, "Directory  Created");
            }
        }
    dm.remove(downloadId);

    //boolean a = from.renameTo(to2);

    try { 
        FileUtils.copyFile(from, to2);
    } catch (Exception e) {
        Log.d(LCAT, "rename success? " + e.getMessage());
    }

Solution

  • i think you can not delete the cache of downloadmanager, i think it clears itself automatically after you removed the downloadid from its database but if you like to have a control to your file you can set destination folder to your private external storage and then after download completed delete it.

    File rootDirectory = new File(getActivity().getExternalFilesDir(null).getAbsoluteFile().toString());
    if(!rootDirectory.exists()){
       rootDirectory.mkdirs();
    }  
    req.setDestinationInExternalFilesDir(getActivity(),null ,"/" + fileName);
    

    and after your download completed you can delete it like:

     from.delete();
    

    and the doc says:

    public int remove (long... ids)

    Cancel downloads and remove them from the download manager. Each download will be stopped if it was running, and it will no longer be accessible through the download manager. If there is a downloaded file, partial or complete, it is deleted.

    http://developer.android.com/reference/android/app/DownloadManager.html#remove%28long...%29