gpt4 book ai didi

java - 如何从webview下载文件

转载 作者:行者123 更新时间:2023-12-02 06:09:54 24 4
gpt4 key购买 nike

我无法从我的网站下载在 WebView 中加载的文件。单击文件的下载链接后,Web View 会重新加载主页 URL。

我通过一些博客帮助更改了一些代码,但没有改变。还可以帮助编写从网站下载文件的代码。

public class WebPageActivity extends AppCompatActivity {

private WebView webView;

public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_web_page);
webView = findViewById(R.id.webView);

// Define url that will open in webview
String webViewUrl = "http://www.tekies.online";

webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
webView.setScrollbarFadingEnabled(false);
// webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setPluginState(PluginState.ON);
webView.getSettings().setAllowFileAccess(true);
webView.getSettings().setSupportZoom(true);

if(!isConnected(WebPageActivity.this)) buildDialog(WebPageActivity.this).show();
else {
webView.loadUrl(webViewUrl);
startWebView();
}


webView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});
}

private void startWebView() {

webView.setWebViewClient(new WebViewClient() {
ProgressDialog progressDialog;

public boolean shouldOverrideUrlLoading(WebView view, String url) {
if (url.contains("youtube")) {
view.getContext().startActivity(
new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
// Open links in External Browser
return true;

} else {
// Stay within this webview and load url
view.loadUrl(url);
return true;
}

}


public void onLoadResource(WebView view, String url) {
if (progressDialog == null && url.contains("tekies")
) {
progressDialog = new ProgressDialog(WebPageActivity.this);
progressDialog.setMessage("Loading...");
progressDialog.show();
}
}
public void onPageFinished(WebView view, String url) {

try {
// Close progressDialog
if (progressDialog.isShowing()) {
progressDialog.dismiss();
progressDialog = null;
}
} catch (Exception exception) {
exception.printStackTrace();
}
}

});

}

@Override
public void onBackPressed() {
if(webView.canGoBack()) {
webView.goBack();
} else {
super.onBackPressed();
}
}

public boolean isConnected(Context context) {

ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netinfo = cm.getActiveNetworkInfo();

if (netinfo != null && netinfo.isConnectedOrConnecting()) {
android.net.NetworkInfo wifi = cm.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
android.net.NetworkInfo mobile = cm.getNetworkInfo(ConnectivityManager.TYPE_MOBILE);

if((mobile != null && mobile.isConnectedOrConnecting()) || (wifi != null && wifi.isConnectedOrConnecting())) return true;
else return false;
} else
return false;
}

public AlertDialog.Builder buildDialog(Context c) {

AlertDialog.Builder builder = new AlertDialog.Builder(c);
Toast.makeText(getApplicationContext(), "Please Connect to Internet", Toast.LENGTH_SHORT).show();
builder.setMessage("You need to have Mobile Data or wifi to access this. Press ok to connect");

builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
Intent dialogIntent = new Intent(android.provider.Settings.ACTION_SETTINGS);
dialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(dialogIntent);
}
});

return builder;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case R.id.menu_item1:
Intent intent = new Intent(this, settings.class);
this.startActivity(intent);
break;
case R.id.menu_item2:
// another startActivity, this is for item with id "menu_item2"
break;
default:
return super.onOptionsItemSelected(item);
}
return true;
}
}

必须从 WebView 下载文件。

最佳答案

你可以试试这个

mWebView.setDownloadListener(new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Intent i = new Intent(Intent.ACTION_VIEW);
i.setData(Uri.parse(url));
startActivity(i);
}
});

需要在Manifest中添加此权限。

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

继续编码:)

关于java - 如何从webview下载文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55917050/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com