下载同一个文件? [英] Downloads the same file twice?

查看:191
本文介绍了下载同一个文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好我一直在寻找通过我的code的各个部分,试图找出发生了什么,但似乎无法推测出来。下面code应该是下载两个文件一个名为clientraw和一个叫clientrawextra但由于某些原因,当我在目录中查找有2个版本,每个文件clientraw ... 1 ... clientrawextra ... 1 ...

因此​​,好像它下载文件多次,我不知道为什么?

在此先感谢!

 距离dis =新的距离();
        dis.findURL(值);
    字符串URL = dis.findURL(值);
    DownloadManager.Request要求=新DownloadManager.Request(Uri.parse(URL));
    //为了这个,如果要运行,则必须使用Android 3.2的编译您的应用程序
    如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.HONEYCOMB){
        request.allowScanningByMediaScanner();
    }
    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADSclientraw.txt);    //获取下载服务和排队文件
    下载管理经理=(下载管理器)getSystemService(Context.DOWNLOAD_SERVICE);
    manager.enqueue(请求);    ////////////////////////////////////////////////// ///
    距离disextra =新的距离();
    disextra.findextra(值);
    字符串urlextra = disextra.findextra(值);DownloadManager.Request requestextra =新DownloadManager.Request(Uri.parse(urlextra));
//为了这个,如果要运行,则必须使用Android 3.2的编译您的应用程序
如果(Build.VERSION.SDK_INT> = Build.VERSION_ codeS.HONEYCOMB){
    requestextra.allowScanningByMediaScanner();
}
requestextra.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADSclientrawextra.txt);manager.enqueue(requestextra);
mDownload =新DownLoadComplte();
registerReceiver(mDownload,新的IntentFilter(
        DownloadManager.ACTION_DOWNLOAD_COMPLETE));

和广播接收机...

 私有类DownLoadComplte扩展广播接收器{
    @覆盖
    公共无效的onReceive(上下文的背景下,意图意图){        如果(intent.getAction()。equalsIgnoreCase(
                DownloadManager.ACTION_DOWNLOAD_COMPLETE)){
            意图myIntent =新意图(splash.this,MainActivity.class);
            myIntent.putExtra(钥匙,值); //可选参数
            myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
            // unregisterReceiver(mDownload);
            splash.this.startActivity(myIntent);
        }
    }
}


解决方案

因此​​,如果任何人有同样的问题,显然这是一个持续的,并与下载管理器尚未解决的问题。我已经使用在其周围,你可能需要使用,如果你的流程类似于矿工作的一点点。基本上每个用户打开应用两个文件时会自动下载到这将覆盖这两个文件previously下载SD卡。所以,我所做的只是添加一些额外的功能来删除重复...

 文件SD卡= Environment.getExternalStorageDirectory();
     档案文件=新的文件(sdCard.getAbsolutePath()+
            /下载/,客户端);
     Log.d(文件路径,将String.valueOf(文件));
        如果(file.exists())
        {
            布尔标志= file.delete();
            Log.d(文件,删除文件+标志);
        }
        文件sdCardextra = Environment.getExternalStorageDirectory();
        文件fil​​eextra =新的文件(sdCardextra.getAbsolutePath()+
                /下载/,clientextra);
        布尔存在= fileextra.exists();
        Log.d(下称文件存在=,将String.valueOf(存在));
           如果(fileextra.exists())
           {
            布尔标志= fileextra.delete();
            Log.d(文件,删除文件+标志);
           }           文件sdCard2 = Environment.getExternalStorageDirectory();
           档案文件2 =新的文件(sdCard2.getAbsolutePath()+
                /下载/,clientraw-的1.txt);
           Log.d(文件路径,将String.valueOf(文件2));
              如果(file2.exists())
              {
                布尔标志= file2.delete();
                Log.d(文件,删除文件+标志);
              }
              文件sdCardextra3 = Environment.getExternalStorageDirectory();
              文件fil​​eextra3 =新的文件(sdCardextra3.getAbsolutePath()+
                    /下载/,clientrawextra-的1.txt);
              布尔存在= fileextra3.exists();
              Log.d(下称文件存在=,将String.valueOf(存在));
                 如果(fileextra3.exists())
                 {
                    布尔标志= fileextra3.delete();
                    Log.d(文件,删除文件+标志);
                 }

Hi i've been looking through various parts of my code to try and find out whats happening but can't seem to figure it out. The following code is supposed to be downloading two files one called "clientraw" and one called "clientrawextra" but for some reason when i look in the directory there are 2 versions of each file "clientraw...1..." "clientrawextra...1..."

Therefore it seems like it's downloading the files multiple times and i have no idea why??

Thanks in advance!

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

And the broadcast receiver...

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

解决方案

So if anyone has the same issue, apparently this is an ongoing and as of yet unresolved issue with the Download manager. I have used a little bit of a work around which you might want to use if your flow is similar to mine. Basically every time that the user opens the app two files are automatically downloaded to the SD card which overwrites the two files previously downloaded. So all i did was add a couple of extra functions to delete the duplicates...

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

这篇关于下载同一个文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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