我想知道已经有两天了,但没能修好它。
我在WebView的内部打开了一个URL。网站中有一个Button可以下载.cer文件。
我的问题是,当我点击那个Button时,它开始下载文件并在Notification Bar中通知我。
我在下面的代码中尝试这样做:
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(sourceURL));
// appears the same in Notification bar while downloading
request.setTitle("Downloading");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
//DownloadManager.Request request = new DownloadManager.Request(source);
manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
// Use the same file name for the destination
File destinationFile = new File(directory, "/Download/test.cer");
if (destinationFile.exists()) {
destinationFile.delete();
}
request.setDestinationUri(Uri.fromFile(destinationFile));
// Add it to the manager
referencepath = manager.enqueue(request);设备的默认浏览器也是如此,但当我在firefox浏览器中打开url时,我可以下载文件。
我还增加了权限
<!-- Permission so that application can access Internet -->
<uses-permission android:name="android.permission.INTERNET" />
<!-- Permission so that application can write on device storage -->
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />在清单文件中。
请帮帮我。
发布于 2014-04-11 05:00:30
嗨,我终于成功下载了.cer文件。
对我起作用的代码是:
String sourceURL = url;
String directoryPath = directoryPath;
DefaultHttpClient client = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(sourceURL);
try {
/**
* getting Session Id from URL which is in Webview
*/
String sessionId = CookieManager.getInstance().getCookie(
sourceURL.toString());
if (sessionId != null)
httpPost.addHeader("Cookie", sessionId);
HttpResponse execute = client.execute(httpPost);
String certResponseBody = EntityUtils.toString(execute
.getEntity());我将.cer文件作为certResponseBody中的字符串保存为设备存储中的.cer文件。
干杯..。
https://stackoverflow.com/questions/22836673
复制相似问题