无法使用下载管理器下载文件

时间:2019-07-25 12:35:38

标签: android android-download-manager

我正在使用下载管理器来下载文件,但是当我尝试使用android pie设备下载文件时,它不起作用。

有人可以告诉我什么是替代方法。

我还尝试在清单文件中添加网络配置文件,这是我尝试解决此问题时所看到的解决方案之一。

1 个答案:

答案 0 :(得分:0)

对于Android 8及更高版本,您必须像下面这样编码

Create a folder under res- xml and create a file named -provider_paths

将此代码添加到provider_paths.xml

<?xml version="1.0" encoding="utf-8"?>
 <paths>
    <external-path path="Android/data/YOUR_PACKAGE_NAME/" name="files_root" />
    <external-path path="." name="external_storage_root" />
</paths>

在Manifest.xml中添加以下代码

<provider
   android:name="android.support.v4.content.FileProvider"
   android:authorities="YOUR_PACKAGE_NAME.fileProvider"
   android:exported="false"
   android:grantUriPermissions="true">
  <meta-data
    android:name="android.support.FILE_PROVIDER_PATHS"
    android:resource="@xml/provider_paths" />
</provider>

创建一个异步任务以从服务器下载文件

private class DownloadFile extends AsyncTask<String, Void, Void> {

@Override
protected void onPreExecute() {
    super.onPreExecute();

    downloadDialog = new ProgressDialog(context);
    downloadDialog.setCancelable(false);
    downloadDialog.setMessage(context.getString(R.string.please_wait));
    downloadDialog.show();
}

@Override
protected Void doInBackground(String... strings) {
    String fileUrl = strings[0]; 
    String fileName = strings[1]; 
    String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
    File folder = new File(extStorageDirectory, context.getString(R.string.directory));
    folder.mkdir();

    pdfFile = new File(folder, fileName);

    try {
        pdfFile.createNewFile();

    } catch (IOException e) {
        e.printStackTrace();
    }
    FileDownloader.downloadFile(fileUrl, pdfFile);
    return null;
}

@Override
protected void onPostExecute(Void aVoid) {
    super.onPostExecute(aVoid);
    downloadDialog.dismiss();
    showMessageWithAction(context.getString(R.string.file_downloaded), pdfFile);
}
}

在AsycTask上方执行new DownloadFile().execute("FILE_URL", "FILE_NAME");

要查看文件,请添加以下代码

private void browseFile(File fileName) {
try {
    MimeTypeMap mime = MimeTypeMap.getSingleton();
    String ext = fileName.getName().substring(fileName.getName().lastIndexOf(".") + 1);
    String type = mime.getMimeTypeFromExtension(ext);
    try {
        Intent intent = new Intent();
        intent.setAction(Intent.ACTION_VIEW);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            //change file provider name here
            Uri contentUri = FileProvider.getUriForFile(Perspective.this, "YOUR_PACKAGE_NAME.fileProvider", fileName);
            intent.setDataAndType(contentUri, type);
        } else {
            intent.setDataAndType(Uri.fromFile(fileName), type);
        }
        startActivity(intent);
    } catch (ActivityNotFoundException anfe) {
        Toast.makeText(Perspective.this, "No activity found to open this attachment.", Toast.LENGTH_LONG).show();
    }
} catch (Exception e) {
    e.printStackTrace();
}
 }

最后的更改,在您的项目中添加此类

public class FileDownloader {
private static final int  MEGABYTE = 1024 * 1024;

public static void downloadFile(String fileUrl, File directory){
    try {

        URL url = new URL(fileUrl);
        HttpURLConnection urlConnection = (HttpURLConnection)url.openConnection();
        //urlConnection.setRequestMethod("GET");
        //urlConnection.setDoOutput(true);
        urlConnection.connect();

        InputStream inputStream = urlConnection.getInputStream();
        FileOutputStream fileOutputStream = new FileOutputStream(directory);
        int totalSize = urlConnection.getContentLength();

        byte[] buffer = new byte[MEGABYTE];
        int bufferLength = 0;
        while((bufferLength = inputStream.read(buffer))>0 ){
            fileOutputStream.write(buffer, 0, bufferLength);
        }
        fileOutputStream.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
}

希望这会对您有所帮助。