DownloadManager.Request request = new DownloadManager.Request(uri);
request.setDestinationInExternalPublicDir(Environment.
DIRECTORY_DOWNLOADS, nameOfFile)
打开它
File file = new File(Environment.
DIRECTORY_DOWNLOADS, nameOfFile);
MimeTypeMap map = MimeTypeMap.getSingleton();
String ext = MimeTypeMap.getFileExtensionFromUrl(file.getName());
String type = map.getMimeTypeFromExtension(ext);
但我收到一条错误消息,指出无法访问该文件。检查位置
答案 0 :(得分:1)
尝试使用读取权限:
android.permission.READ_EXTERNAL_STORAGE
答案 1 :(得分:0)
试试这个......
protected void openFile(String fileName) {
Intent install = new Intent(Intent.ACTION_VIEW);
install.setDataAndType(Uri.fromFile(new File(fileName)),
"MIME-TYPE");
startActivity(install);
}
答案 2 :(得分:0)
这是一个有效的解决方案。注意:不要使用DownloadManager.COLUMN_LOCAL_FILENAME,因为它在API 24中已弃用。请改用DownloadManager.COLUMN_LOCAL_URI。
创建字段下载管理器和长变量以保存下载ID。
DownloadManager dm;
long downloadId;
String pendingDownloadUrl = url;
fial int storagePermissionRequestCode = 101;
创建下载管理器
dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
注册广播接收器以便下载完成
BroadcastReceiver downloadCompleteReceiver = new BroadcastReceiver() {
@Override
public void onReceive(final Context context, final Intent intent) {
Cursor c = dm.query(new DownloadManager.Query().setFilterById(downloadId));
if (c != null) {
c.moveToFirst();
try {
String fileUri = c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
File mFile = new File(Uri.parse(fileUri).getPath());
String fileName = mFile.getAbsolutePath();
openFile(fileName);
}catch (Exception e){
Log.e("error", "Could not open the downloaded file");
}
}
}
};
//注册boradcast以下载完成
registerReceiver(downloadCompleteReceiver, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
开始下载
private void onDownloadStart(String url) {
if (ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED) {
downloadFile(url);
} else {
pendingDownloadUrl = url;
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, storagePermissionRequestCode);
} }
//使用下载管理器下载文件
private void downlaodFile(String url){
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(url));
String filename = URLUtil.guessFileName(url, null, null);
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS,filename);
downloadId = dm.enqueue(request);//save download id for later reference }
//权限状态
@Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if(requestCode == storagePermissionRequestCode){
boolean canDownload = true;
for (int grantResult : grantResults) {
if (grantResult == PackageManager.PERMISSION_DENIED) {
canDownload = false;
break;
}
}
if(canDownload){
downlaodFile(pendingDownloadUrl);
}
} }
打开下载的文件
private void openFile(String file) {
try {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setDataAndType(Uri.fromFile(new File(file)), "application/pdf");//this is for pdf file. Use appropreate mime type
startActivity(i);
} catch (Exception e) {
Toast.makeText(this,"No pdf viewing application detected. File saved in download folder",Toast.LENGTH_SHORT).show();
}
}
现在尝试通过调用downladFile(String url);
方法