两次下载相同的文件?

时间:2013-09-18 18:29:06

标签: 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); 
        }
    }
}

1 个答案:

答案 0 :(得分:2)

因此,如果有人遇到同样的问题,显然这是一个正在进行且尚未解决的下载管理器问题。如果你的流程类似于我的话,我已经使用了一些你可能想要使用的工作。基本上每次用户打开应用程序时,两个文件都会自动下载到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);
                 }