下载管理器完成下载后无法移动文件 [英] Can't move file after DownloadManager completes download

查看:605
本文介绍了下载管理器完成下载后无法移动文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的Andr​​oid应用我使用的系统下载管理器下载的照片,一旦收到我处理它,然后我需要将它移动到另一个位置。如果我只是把它复制它工作正常,如果我尝试移动/删除它,我得到一个异常有关,不能移动。

 复制失败后删除原文件/data/user/0/com.android.providers.downloads/cache/a3133p2930-9.jpg'到'/数据/数据​​/ com.XXXXX.testharness / app_appdata /图像/一/二/三维/ 6 / a3133p2930.jpg

也试过:

1)下载管理器试图移动之前将其删除
2)直接设定目标到目标目录,但得到一个错误,我只能使用公共的外部文件夹。
3)尝试使用JAVA renameTo和Apache公地moveFile

这是一个code片段,显示我在做什么的相关部分。

什么想法?

AP preciated

  @覆盖
公共无效的onReceive(上下文的背景下,意图意图){    字符串行动= intent.getAction();
    如果(DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(动作)){
    字符串uriString中= c.getString(
                        c.getColumnIndex(
                                DownloadManager.COLUMN_LOCAL_URI));
    字符串uri2String = c.getString(
                            c.getColumnIndex(
                                    DownloadManager.COLUMN_URI));    字符串localFileString = c.getString(
                                c.getColumnIndex(
                                        DownloadManager.COLUMN_LOCAL_FILENAME));    从=新的文件(localFileString)文件;
    文件以=新的文件(appDataDir,localString);
    文件TO2 =新的文件(appDataDir,localString +/+ getFileNameFromUrl(uri2String));
    如果(!to.exists()){
        如果(to.mkdirs()){
            Log.d(LCAT,目录下创建);
            }
        }
    dm.remove(downloadId);    //布尔A = from.renameTo(TO2);    尝试{
        FileUtils.copyFile(从,TO2);
    }赶上(例外五){
        Log.d(LCAT,重命名成功了吗?+ e.getMessage());
    }


解决方案

我想你不能删除下载管理器的缓存,我想它会自动清除自身后删​​除从其数据库中downloadid,但如果你想有一个控制你的文件,你可以目标文件夹设置为您的私人外部存储,然后下载后完成删除。

 文件根目录=新的文件(getActivity()getExternalFilesDir(空).getAbsoluteFile()的toString());
如果(!rootDirectory.exists()){
   rootDirectory.mkdirs();
}
req.setDestinationInExternalFilesDir(getActivity(),NULL,/+文件名);

和后您的下载完成后,您可以将其删除,如:

  from.delete();

和医生说:


  

公众诠释删除(长... IDS)


  
  

取消下载并删除它们
  下载管理器。如果正在运行每次下载都将停止,
  它将不再是通过下载管理器访问。如果
  有一个下载的文件,部分或完全的,它会被删除。


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

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());
    }

解决方案

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

这篇关于下载管理器完成下载后无法移动文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆