简体   繁体   English

下载管理器错误文件错误

[英]Download Manager Error File Error

Im using the Download Manager API to download files from a server through a HTTP Request. 我正在使用Download Manager API通过HTTP请求从服务器下载文件。 This method works perfectly file for Android API version < 11. I need to implement it for Android API version 10 (GingerBread). 对于Android API版本<11,此方法可以完美运行。我需要针对Android API版本10(GingerBread)实施该方法。 In this one, it is giving ERROR_FILE_ERROR. 在这一步中,它给出了ERROR_FILE_ERROR。 Please advice. 请指教。

   Uri uri = Uri.parse(serverConnection.url + serverConnection.studentSearchService +        "GetAssignmentFile/" + serverConnection.connectionString + fileList.get(i).getFileId());



        final DownloadManager downloadManager = (DownloadManager) getActivity().getSystemService(Context.DOWNLOAD_SERVICE);
        DownloadManager.Request downloadReq = new DownloadManager.Request(uri);
        downloadReq
                .setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI
                        | DownloadManager.Request.NETWORK_MOBILE);
      //  downloadReq.allowScanningByMediaScanner();
        downloadReq.setMimeType(fileList.get(i).getFileType());
        downloadReq.setTitle(fileList.get(i).getFileName());

            downloadReq.setDescription("attachment");
            downloadReq.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileList.get(i).getFileName());
 if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.HONEYCOMB) {

                 downloadReq.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE | DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);

            }
            else
            {
                downloadReq.setShowRunningNotification(true);
               downloadReq.setVisibleInDownloadsUi(true);
            }


            myDownloadReference = downloadManager.enqueue(downloadReq);


        IntentFilter intentFilter= new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
        receiverDownloadComplete=new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String extraId= DownloadManager.EXTRA_DOWNLOAD_ID;
                long reference=intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID,-1);

                    if(reference==myDownloadReference)
                    {
                        //Toast.makeText(getActivity(),"Downloaded",Toast.LENGTH_SHORT).show();

                        DownloadManager.Query query= new DownloadManager.Query();
                        query.setFilterById(reference);
                        Cursor cursor=downloadManager.query(query);
                        cursor.moveToFirst();
                        int columnIndex=cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
                        int status=cursor.getInt(columnIndex);

                        int columnReason=cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
                        int reason=cursor.getInt(columnReason);
                        switch(status)
                        {
                            case DownloadManager.STATUS_SUCCESSFUL:

                                Toast.makeText(getActivity(),"File Saved at:"+fileSavedPath,Toast.LENGTH_SHORT).show();
                                break;

                            case DownloadManager.STATUS_FAILED:


                           String failedReason="";

                                        switch(reason){
                                            case DownloadManager.ERROR_CANNOT_RESUME:
                                                failedReason = "ERROR_CANNOT_RESUME";
                                                break;
                                            case DownloadManager.ERROR_DEVICE_NOT_FOUND:
                                                failedReason = "ERROR_DEVICE_NOT_FOUND";
                                                break;
                                            case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
                                                failedReason = "ERROR_FILE_ALREADY_EXISTS";
                                                break;
                                            case DownloadManager.ERROR_FILE_ERROR:
                                                failedReason = "ERROR_FILE_ERROR";
                                                break;
                                            case DownloadManager.ERROR_HTTP_DATA_ERROR:
                                                failedReason = "ERROR_HTTP_DATA_ERROR";
                                                break;
                                            case DownloadManager.ERROR_INSUFFICIENT_SPACE:
                                                failedReason = "ERROR_INSUFFICIENT_SPACE";
                                                break;
                                            case DownloadManager.ERROR_TOO_MANY_REDIRECTS:
                                                failedReason = "ERROR_TOO_MANY_REDIRECTS";
                                                break;
                                            case DownloadManager.ERROR_UNHANDLED_HTTP_CODE:
                                                failedReason = "ERROR_UNHANDLED_HTTP_CODE";
                                                break;
                                            case DownloadManager.ERROR_UNKNOWN:
                                                failedReason = "ERROR_UNKNOWN";
                                                break;
                                        }


                                Toast.makeText(getActivity(),"Download failed because "+failedReason,Toast.LENGTH_SHORT).show();

                                break;

                        }



                }
            }
        };
        getActivity().registerReceiver(receiverDownloadComplete,intentFilter);


    }

It gives Download Failed, ERROR_FILE_ERROR 它显示下载失败,ERROR_FILE_ERROR

It's been some time, but just for the record. 已经有一段时间了,但是只是为了记录。 This also happened to me on a Nexus 4 with Android KitKat 4.4.4. 我在配备Android KitKat 4.4.4的Nexus 4上也遇到了这种情况。 However, on a Nexus 5 & 6 with Android Lollipop I do not see this error. 但是,在具有Android Lollipop的Nexus 5和Nexus 6上,我没有看到此错误。 In my case, the solution was to remove the folder name from the second parameter in setDestinationInExternalPublicDir . 就我而言,解决方案是从setDestinationInExternalPublicDir的第二个参数中删除文件夹名称。 Instead of this: 代替这个:

downloadReq.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "myFolder/fileName.txt");

Use this: 用这个:

downloadReq.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "fileName.txt");

I have the some problem. 我有一些问题。 To solve this, make sure parent directory of your destination file is exist. 要解决此问题,请确保目标文件的父目录存在。

fileList.get(i).getParentFile().mkdirs()   //make sure parent directory is exist
downloadReq.setDescription("attachment");
downloadReq.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileList.get(i).getFileName());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM