Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 下载管理器错误文件错误_Android_Android 2.3 Gingerbread_Android Download Manager - Fatal编程技术网

Android 下载管理器错误文件错误

Android 下载管理器错误文件错误,android,android-2.3-gingerbread,android-download-manager,Android,Android 2.3 Gingerbread,Android Download Manager,Im使用下载管理器API通过HTTP请求从服务器下载文件。对于Android API版本

Im使用下载管理器API通过HTTP请求从服务器下载文件。对于Android API版本<11的文件,此方法非常有效。我需要为Android API版本10姜饼实现它。在这个例子中,它给出了ERROR\u FILE\u ERROR。请给我一些建议

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


    }

它给出了下载失败,错误文件错误,已经有一段时间了,但只是为了记录。这也发生在我使用Android KitKat 4.4.4的Nexus4上。然而,在带有Android棒棒糖的Nexus5和6上,我没有看到这个错误。在我的例子中,解决方案是从setDestinationNexternalPublicDir中的第二个参数中删除文件夹名称。 与此相反:

downloadReq.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "myFolder/fileName.txt");
使用以下命令:

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

已经有一段时间了,但只是为了记录在案。这也发生在我使用Android KitKat 4.4.4的Nexus4上。然而,在带有Android棒棒糖的Nexus5和6上,我没有看到这个错误。在我的例子中,解决方案是从setDestinationNexternalPublicDir中的第二个参数中删除文件夹名称。 与此相反:

downloadReq.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "myFolder/fileName.txt");
使用以下命令:

downloadReq.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "fileName.txt");
我有一些问题。 要解决此问题,请确保目标文件的父目录存在

fileList.get(i).getParentFile().mkdirs()   //make sure parent directory is exist
downloadReq.setDescription("attachment");
downloadReq.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileList.get(i).getFileName());
我有一些问题。 要解决此问题,请确保目标文件的父目录存在

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