[英]Not able to download files using download manager
I'm using download manager for downloading files but when I tried to download file using android pie devices it doesn't work.我正在使用下载管理器下载文件,但是当我尝试使用 android pie 设备下载文件时,它不起作用。
Can some one tell me what is the alternative for this.有人可以告诉我什么是替代方案。
I have also tried adding network config file in the manifest file which was one of the solution i saw when i tried to solve the issue.我还尝试在清单文件中添加网络配置文件,这是我在尝试解决问题时看到的解决方案之一。
For Android 8 and above you have to code like below对于 Android 8 及更高版本,您必须编写如下代码
Create a folder under res- xml and create a file named -provider_paths
Add this code to provider_paths.xml将此代码添加到 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>
Add below code in Manifest.xml在 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>
Create a Async Task to download file from Server创建异步任务以从服务器下载文件
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);
}
}
Excute above AsycTask by new DownloadFile().execute("FILE_URL", "FILE_NAME");
通过
new DownloadFile().execute("FILE_URL", "FILE_NAME");
To view file add below code要查看文件添加以下代码
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();
}
}
Final changes, add this class in your project最终更改,在您的项目中添加此类
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();
}
}
}
Hope this will help you.希望这会帮助你。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.