webview下载应用程序但没有下载
webview downloading app but no downloading
我是 webview 应用程序的菜鸟而且 java 也是
我想创建一个用于下载网站的 webview-app-downloadgram.com 网站
但是加载照片后它没有开始下载。请告诉我解决方案。
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.INTERNET" />
我的网站让例子 -- downloadgram.com
我的 mainactivity.java 是
public class MainActivity extends AppCompatActivity {
private WebView mywebView;
private static final String TAG = "MainActivity";
private static final int REQUEST_CODE=2;.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebView=(WebView) findViewById(R.id.webview);
mywebView.setWebViewClient(new WebViewClient());
WebSettings webSettings=mywebView.getSettings();
mywebView.getSettings().setJavaScriptEnabled(true);
mywebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mywebView.getSettings().setDomStorageEnabled(true);
mywebView.setWebChromeClient(new WebChromeClient());
mywebView.setWebViewClient(new WebViewClient());
mywebView.loadUrl("https://downloadgram.com/");
isWriteStoragePermissionGranted();
}
public class mywebClient extends WebViewClient{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
super.onPageStarted(view,url,favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view,String url){
view.loadUrl(url);
return true;
}
}
@Override
public void onBackPressed(){
if(mywebView.canGoBack()) {
mywebView.goBack();
}
else{
super.onBackPressed();
}
}
public boolean isReadStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Permission is granted1");
return true;
} else {
Log.v(TAG,"Permission is revoked1");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 3);
return false;
}
}
else {
Log.v(TAG,"Permission is granted1");
return true;
}
}
public boolean isWriteStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Permission is granted2");
return true;
} else {
Log.v(TAG,"Permission is revoked2");
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, 2);
return false;
}
}
else {
Log.v(TAG,"Permission is granted2");
return true;
}
}
}
为什么下载有问题...??
请给我一个解决方案...
您需要将 DownloadListener
添加到 Webview 以从 Webview 获取下载 link,然后将下载 link 传递给 DownloadManager
mywebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "newImage.jpg");//replace newImage.jpg
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(), "File downloading",Toast.LENGTH_LONG).show();
}
});
你也可以试试这个,只需要在onClickListener中使用这段代码
String DownloadImageURL = webViewHitTestResult.getExtra();
if(URLUtil.isValidUrl(DownloadImageURL)){
DownloadManager.Request mRequest = new DownloadManager.Request(Uri.parse(DownloadImageURL));
mRequest.allowScanningByMediaScanner();
mRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
DownloadManager mDownloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
mDownloadManager.enqueue(mRequest);
}
我是 webview 应用程序的菜鸟而且 java 也是
我想创建一个用于下载网站的 webview-app-downloadgram.com 网站 但是加载照片后它没有开始下载。请告诉我解决方案。
<uses-permission
android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.INTERNET" />
我的网站让例子 -- downloadgram.com
我的 mainactivity.java 是
public class MainActivity extends AppCompatActivity {
private WebView mywebView;
private static final String TAG = "MainActivity";
private static final int REQUEST_CODE=2;.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mywebView=(WebView) findViewById(R.id.webview);
mywebView.setWebViewClient(new WebViewClient());
WebSettings webSettings=mywebView.getSettings();
mywebView.getSettings().setJavaScriptEnabled(true);
mywebView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
mywebView.getSettings().setDomStorageEnabled(true);
mywebView.setWebChromeClient(new WebChromeClient());
mywebView.setWebViewClient(new WebViewClient());
mywebView.loadUrl("https://downloadgram.com/");
isWriteStoragePermissionGranted();
}
public class mywebClient extends WebViewClient{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon){
super.onPageStarted(view,url,favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view,String url){
view.loadUrl(url);
return true;
}
}
@Override
public void onBackPressed(){
if(mywebView.canGoBack()) {
mywebView.goBack();
}
else{
super.onBackPressed();
}
}
public boolean isReadStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Permission is granted1");
return true;
} else {
Log.v(TAG,"Permission is revoked1");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 3);
return false;
}
}
else {
Log.v(TAG,"Permission is granted1");
return true;
}
}
public boolean isWriteStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Permission is granted2");
return true;
} else {
Log.v(TAG,"Permission is revoked2");
ActivityCompat.requestPermissions(this, new String[] {Manifest.permission.WRITE_EXTERNAL_STORAGE}, 2);
return false;
}
}
else {
Log.v(TAG,"Permission is granted2");
return true;
}
}
}
为什么下载有问题...?? 请给我一个解决方案...
您需要将 DownloadListener
添加到 Webview 以从 Webview 获取下载 link,然后将下载 link 传递给 DownloadManager
mywebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
DownloadManager.Request request = new DownloadManager.Request(
Uri.parse(url));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "newImage.jpg");//replace newImage.jpg
DownloadManager dm = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
dm.enqueue(request);
Toast.makeText(getApplicationContext(), "File downloading",Toast.LENGTH_LONG).show();
}
});
你也可以试试这个,只需要在onClickListener中使用这段代码
String DownloadImageURL = webViewHitTestResult.getExtra();
if(URLUtil.isValidUrl(DownloadImageURL)){
DownloadManager.Request mRequest = new DownloadManager.Request(Uri.parse(DownloadImageURL));
mRequest.allowScanningByMediaScanner();
mRequest.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
DownloadManager mDownloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
mDownloadManager.enqueue(mRequest);
}