Android下载管理器使用HTTP_DATA_ERROR重试并失败

问题描述 投票:0回答:1
 class LongOperation extends AsyncTask<String, Void, String> {

                            @Override
                            protected String doInBackground(String... params) {

                                try {
                                    String url2 = "https://www.w3schools.com/w3css/img_fjords.jpg";
                                    String url = url2.replaceAll(" ", "%20");

                                    String fileName = URLUtil.guessFileName(url, null, MimeTypeMap.getFileExtensionFromUrl(url));

                                    DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));

                                    request.setMimeType(MimeTypeMap.getFileExtensionFromUrl(url));
                                    String cookies = CookieManager.getInstance().getCookie(url);
                                    //request.addRequestHeader("Cookie", cookies);
                                    //request.addRequestHeader("User-Agent", userAgent);
                                    request.setDescription("Downloading File");
                                    //request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
                                    request.setTitle("test.png");
                                    request.allowScanningByMediaScanner();
                                    request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
                                    request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, fileName);
                                    DownloadManager downloadManager = (DownloadManager) module.getActivity().getSystemService(DOWNLOAD_SERVICE);
                                    if (downloadManager != null) {
                                        final long downloadID = downloadManager.enqueue(request);
                                        System.out.println(downloadID);


                                    }
                                    Toast.makeText(module.getActivity(), "Downloading File", Toast.LENGTH_LONG).show();
                                } catch (Exception e) {
                                    Log.e(TAG, e.getMessage());
                                }

                                return "Executed";
                            }

                            @Override
                            protected void onPostExecute(String result) {
                                Log.d("Post Execute","Post Execute");
                                // might want to change "executed" for the returned string passed
                                // into onPostExecute() but that is upto you
                            }

                            @Override
                            protected void onPreExecute() {}

                            @Override
                            protected void onProgressUpdate(Void... values) {}
                        }
                        new LongOperation().execute("");

我正在使用上面的代码在我的本机应用程序中使用本机android代码下载图像。当此方法执行时,下载状态将进入名为STATUS_PAUSED - > PAUSED_WAITING_TO_RETRY的状态,并在一段时间后失败并显示错误代码ERROR_HTTP_DATA_ERROR。我在我的android清单中给出了以下权限,并且在运行时我也请求了存储权限。

<uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

我究竟做错了什么?

android react-native android-download-manager imagedownload
1个回答
0
投票

将此代码放在异步任务之外,位于您的活动/服务中的某个位置。该文件将下载到/ sdcard0 / Android / data / your_package_name / folder /

String fileName = URLUtil.guessFileName(url, null, MimeTypeMap.getFileExtensionFromUrl(url));
DownloadManager downloadManager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(Uri.parse("https://www.w3schools.com/w3css/img_fjords.jpg"));
request.setDestinationInExternalFilesDir(getApplicationContext(), "folder/", fileName);
request.setMimeType(MimeTypeMap.getFileExtensionFromUrl("https://www.w3schools.com/w3css/img_fjords.jpg"));
request.setAllowedOverRoaming(true);
request.setVisibleInDownloadsUi(true);
request.setTitle("Downloading Image");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE);
request.setAllowedNetworkTypes(DownloadManager.Request.NETWORK_WIFI | DownloadManager.Request.NETWORK_MOBILE);
long enqueue = downloadManager.enqueue(request);

添加此广播接收器并回复原因

public void setupDownloadBroadcastReceivers() {
        TAG = "CopyADDB";
        Log.d(TAG, "Inside setupDownloadBroadcastReceivers");
        BroadcastReceiver download_receiver = new BroadcastReceiver() {
            @Override
            public void onReceive(Context context, Intent intent) {
                String action = intent.getAction();
                if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
                    long downloadId = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, 0);
                    DownloadManager.Query query = new DownloadManager.Query();
                    query.setFilterById(enqueue);
                    Cursor c = downloadManager.query(query);
                    if (c.moveToFirst()) {
                        int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
                        Log.d(TAG, "Reason is " + c.getInt(columnIndex));
                        if (DownloadManager.STATUS_SUCCESSFUL == c.getInt(columnIndex)) {
                            String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                            String name = c.getString(c.getColumnIndex(DownloadManager.COLUMN_TITLE));
                            Log.d(TAG, "Download Complete");
                            Log.d(TAG, "File Name is " + name);
                            Log.d(TAG, "Path is " + uriString);
                        }
                        if (DownloadManager.STATUS_FAILED == c.getInt(columnIndex)) {
                            String uriString = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
                            Log.d(TAG, "DownloadAppFailed");
                        }
                    }
                }
            }
        };
        registerReceiver(download_receiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
    }
© www.soinside.com 2019 - 2024. All rights reserved.