Android Webview 未触发 href 下载文件
Android Webview not triggering a href download file
我已经添加了下载管理器,但它没有触发下载事件,任何人都可以提供帮助。当用户转到 webview 下载选项并单击 link 下载文件时,没有任何反应。我在 Whosebug 中浏览了很多 post,但对我没有用。
public class MainActivity extends AppCompatActivity {
private WebView mWebView;
// boolean thread_running = true;
private static final String TAG = "MainActivity";
// flag for Internet connection status
Boolean isInternetPresent = false;
// Connection detector class
ConnectionDetector cd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webView);
mWebView.setWebViewClient(new MyAppWebViewClient(this));
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(false);
webSettings.setDomStorageEnabled(true);
// creating connection detector class instance
cd = new ConnectionDetector(getApplicationContext());
/**
* Check Internet status button click event
* */
isInternetPresent = cd.isConnectingToInternet();
// check for Internet status
if (isInternetPresent) {
// Internet Connection is Present
// make HTTP requests
mWebView.loadUrl("http://url");
} else {
// Internet connection is not present
// Ask user to connect to Internet
//showAlertDialog(MainActivity.this, "No Internet Connection",
//"You don't have internet connection.", false);
Toast.makeText(this, "No Internet connection, Please SwitchOn Connection", Toast.LENGTH_LONG).show();
finish();
}
}
@Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
public class MyAppWebViewClient extends WebViewClient {
private Context context;
public MyAppWebViewClient(Context context) {
this.context = context;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (!cd.isConnectingToInternet()) {
//internet is not present
Toast.makeText(getApplicationContext(), "Internet Stopped Working", Toast.LENGTH_SHORT).show();
finish();
}
if(url.contains(".JPG")){
DownloadManager mdDownloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
File destinationFile = new File(Environment.getExternalStorageDirectory(),getFileName(url));
request.setDescription("Downloading ...");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationUri(Uri.fromFile(destinationFile));
mdDownloadManager.enqueue(request);
return true;
}
view.loadUrl(url);
return true;
}
public String getFileName(String url) {
String filenameWithoutExtension = "";
filenameWithoutExtension = String.valueOf(System.currentTimeMillis()
+ ".jpg");
return filenameWithoutExtension;
}
}
}
有人可以帮我吗?页面加载没有问题,但是 HTML 页面中图像文件的 link 不起作用...
点击 link 时出现以下错误
Writing exception to parcel
java.lang.SecurityException: No permission to write to /storage/emulated/0/1499148267411.jpg: Neither user 10109 nor current process has android.permission.WRITE_EXTERNAL_STORAGE.
at android.app.ContextImpl.enforce(ContextImpl.java:1438)
at android.app.ContextImpl.enforceCallingOrSelfPermission(ContextImpl.java:1470)
at com.android.providers.downloads.DownloadProvider.checkFileUriDestination(DownloadProvider.java:724)
at com.android.providers.downloads.DownloadProvider.insert(DownloadProvider.java:558)
at android.content.ContentProvider$Transport.insert(ContentProvider.java:263)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:163)
at android.os.Binder.execTransact(Binder.java:453)
你能显示 html 中的行吗(link 从你下载的地方)
喜欢:
<a href='something'></a>
还是用js触发?
因为如果你愿意,你可以使用这个:
Example
Download file when clicking on the link (instead of navigating to the
file):
<a href="/images/myw3schoolsimage.jpg" download>
更新
将此添加到您的 AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
编辑
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="********">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在清单文件中添加这两行
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
最后,在搜索了很多并阅读了 here 关于 Marshmallow API 23 >= 中的权限之后,我们需要在 运行 时间内授予权限。我用这段代码解决了我的问题,
MainActivity 中的 Oncreate 方法
int permissionCheck = ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
//requesting permission
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
} else {
Log.i(TAG , "Permission Granted");
}
并覆盖此方法
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
//you have the permission now.
Log.i(TAG , "Permission Already Granted");
}
}
我已经添加了下载管理器,但它没有触发下载事件,任何人都可以提供帮助。当用户转到 webview 下载选项并单击 link 下载文件时,没有任何反应。我在 Whosebug 中浏览了很多 post,但对我没有用。
public class MainActivity extends AppCompatActivity {
private WebView mWebView;
// boolean thread_running = true;
private static final String TAG = "MainActivity";
// flag for Internet connection status
Boolean isInternetPresent = false;
// Connection detector class
ConnectionDetector cd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.webView);
mWebView.setWebViewClient(new MyAppWebViewClient(this));
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setSupportZoom(false);
webSettings.setDomStorageEnabled(true);
// creating connection detector class instance
cd = new ConnectionDetector(getApplicationContext());
/**
* Check Internet status button click event
* */
isInternetPresent = cd.isConnectingToInternet();
// check for Internet status
if (isInternetPresent) {
// Internet Connection is Present
// make HTTP requests
mWebView.loadUrl("http://url");
} else {
// Internet connection is not present
// Ask user to connect to Internet
//showAlertDialog(MainActivity.this, "No Internet Connection",
//"You don't have internet connection.", false);
Toast.makeText(this, "No Internet connection, Please SwitchOn Connection", Toast.LENGTH_LONG).show();
finish();
}
}
@Override
public void onBackPressed() {
if (mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
public class MyAppWebViewClient extends WebViewClient {
private Context context;
public MyAppWebViewClient(Context context) {
this.context = context;
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (!cd.isConnectingToInternet()) {
//internet is not present
Toast.makeText(getApplicationContext(), "Internet Stopped Working", Toast.LENGTH_SHORT).show();
finish();
}
if(url.contains(".JPG")){
DownloadManager mdDownloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
File destinationFile = new File(Environment.getExternalStorageDirectory(),getFileName(url));
request.setDescription("Downloading ...");
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationUri(Uri.fromFile(destinationFile));
mdDownloadManager.enqueue(request);
return true;
}
view.loadUrl(url);
return true;
}
public String getFileName(String url) {
String filenameWithoutExtension = "";
filenameWithoutExtension = String.valueOf(System.currentTimeMillis()
+ ".jpg");
return filenameWithoutExtension;
}
}
}
有人可以帮我吗?页面加载没有问题,但是 HTML 页面中图像文件的 link 不起作用...
点击 link 时出现以下错误
Writing exception to parcel
java.lang.SecurityException: No permission to write to /storage/emulated/0/1499148267411.jpg: Neither user 10109 nor current process has android.permission.WRITE_EXTERNAL_STORAGE.
at android.app.ContextImpl.enforce(ContextImpl.java:1438)
at android.app.ContextImpl.enforceCallingOrSelfPermission(ContextImpl.java:1470)
at com.android.providers.downloads.DownloadProvider.checkFileUriDestination(DownloadProvider.java:724)
at com.android.providers.downloads.DownloadProvider.insert(DownloadProvider.java:558)
at android.content.ContentProvider$Transport.insert(ContentProvider.java:263)
at android.content.ContentProviderNative.onTransact(ContentProviderNative.java:163)
at android.os.Binder.execTransact(Binder.java:453)
你能显示 html 中的行吗(link 从你下载的地方) 喜欢:
<a href='something'></a>
还是用js触发?
因为如果你愿意,你可以使用这个:
Example
Download file when clicking on the link (instead of navigating to the file):
<a href="/images/myw3schoolsimage.jpg" download>
更新
将此添加到您的 AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
编辑
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="********">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
在清单文件中添加这两行
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
最后,在搜索了很多并阅读了 here 关于 Marshmallow API 23 >= 中的权限之后,我们需要在 运行 时间内授予权限。我用这段代码解决了我的问题,
MainActivity 中的 Oncreate 方法
int permissionCheck = ActivityCompat.checkSelfPermission(MainActivity.this, android.Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permissionCheck != PackageManager.PERMISSION_GRANTED) {
//requesting permission
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
} else {
Log.i(TAG , "Permission Granted");
}
并覆盖此方法
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
//you have the permission now.
Log.i(TAG , "Permission Already Granted");
}
}