android - 下载同一个文件两次?

标签 android file download android-external-storage

您好,我一直在查看我的代码的各个部分,试图找出发生了什么,但似乎无法弄清楚。以下代码应该下载两个文件,一个名为“clientraw”,一个名为“clientrawextra”,但由于某种原因,当我查看目录时,每个文件都有 2 个版本“clientraw...1...”“clientrawextra” ...1..."

因此它似乎在多次下载文件,我不知道为什么?

提前致谢!

    distance dis = new distance();
        dis.findURL(value);
    String url = dis.findURL(value);


    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
    // in order for this if to run, you must use the android 3.2 to compile your app
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        request.allowScanningByMediaScanner();
    }
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "clientraw.txt");

    // get download service and enqueue file
    DownloadManager manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
    manager.enqueue(request); 

    /////////////////////////////////////////////////////
    distance disextra = new distance();
    disextra.findextra(value);
    String urlextra = disextra.findextra(value);

DownloadManager.Request requestextra = new DownloadManager.Request(Uri.parse(urlextra));
// in order for this if to run, you must use the android 3.2 to compile your app
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
    requestextra.allowScanningByMediaScanner();
}
requestextra.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "clientrawextra.txt");

manager.enqueue(requestextra); 
mDownload = new DownLoadComplte();
registerReceiver(mDownload, new IntentFilter(
        DownloadManager.ACTION_DOWNLOAD_COMPLETE));

广播接收器...

private class DownLoadComplte extends BroadcastReceiver {


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

        if (intent.getAction().equalsIgnoreCase(
                DownloadManager.ACTION_DOWNLOAD_COMPLETE)) {
            Intent myIntent = new Intent(splash.this, MainActivity.class);
            myIntent.putExtra("key", value); //Optional parameters
            myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            //unregisterReceiver(mDownload);
            splash.this.startActivity(myIntent); 
        }
    }
}

最佳答案

因此,如果有人遇到同样的问题,显然这是下载管理器的一个持续且尚未解决的问题。如果您的流程与我的流程相似,您可能想使用一些解决方法。基本上每次用户打开应用程序时,都会自动将两个文件下载到 SD 卡中,这会覆盖之前下载的两个文件。所以我所做的就是添加几个额外的函数来删除重复项...

 File sdCard = Environment.getExternalStorageDirectory();
     File file = new File(sdCard.getAbsolutePath() +
            "/Download/", client);
     Log.d("file path", String.valueOf(file));
        if(file.exists())
        {
            boolean flag = file.delete();
            Log.d("file", "file deleted " + flag);  
        } 


        File sdCardextra = Environment.getExternalStorageDirectory();
        File fileextra = new File(sdCardextra.getAbsolutePath() +
                "/Download/", clientextra);
        boolean exist = fileextra.exists();
        Log.d("the file exists = ", String.valueOf(exist));
           if(fileextra.exists())
           {
            boolean flag = fileextra.delete();
            Log.d("file", "file deleted " + flag);
           } 

           File sdCard2 = Environment.getExternalStorageDirectory();
           File file2 = new File(sdCard2.getAbsolutePath() +
                "/Download/", "clientraw-1.txt");
           Log.d("file path", String.valueOf(file2));
              if(file2.exists())
              {
                boolean flag = file2.delete();
                Log.d("file", "file deleted " + flag);  
              } 


              File sdCardextra3 = Environment.getExternalStorageDirectory();
              File fileextra3 = new File(sdCardextra3.getAbsolutePath() +
                    "/Download/", "clientrawextra-1.txt");
              boolean exists = fileextra3.exists();
              Log.d("the file exists = ", String.valueOf(exists));
                 if(fileextra3.exists())
                 {
                    boolean flag = fileextra3.delete();
                    Log.d("file", "file deleted " + flag);
                 } 

关于android - 下载同一个文件两次?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18879674/

相关文章:

file - 在 Fortran 77 中重命名文件

c - 我认为我超出了此过程的可用内存。有人可以看一下并验证吗?

javascript - 使用 JQuery 触发浏览器下载

java - 下载的文件在用户媒体应用程序中不可见?

android - 如何通过 android 命名空间的 ID 访问 View

android - 无法通过 ListView 检测手势

java - 从文件中获取媒体标题

java - 如何从使用 JavaScript 生成的 dataURL 在 java/android 中创建文件

java - Microsoft Graph/me 不适用于个人帐户

asp.net - C# Asp Net 在 Chrome 12 中下载时出现问题