gpt4 book ai didi

android - 为什么我的下载链接在 Android WebView 中不起作用?

转载 作者:行者123 更新时间:2023-11-30 01:39:45 26 4
gpt4 key购买 nike

在我的 Angular 2 应用程序中,用户可以单击文件进行下载。这将从经过身份验证的 Web 服务调用获取链接,并将其下载到浏览器中。

在浏览器(PC、Android 上的 Chrome 或其他浏览器)上一切正常。

它在带有 WebView 的混合 Android 应用程序中不起作用,尽管我认为我做了所需的一切。似乎从未为生成的链接调用 WebViewClient::onLoadResource。

这是我的 Angular 2 组件中的相关部分:

onPathClicked(path:Path) {
if (path.directory) {
var newPathEntries = this.appendToPath(this.pathEntries, path.fileName);
this.searchContents(newPathEntries);
} else {
var fileName = path.fileName;
var pathEntries = this.pathEntries.push(fileName);
var fullPath = this.buildFullPath(pathEntries);
this.fileService.getFileDownloadUrl(fullPath)
.then((url)=> {
var fileInput:HTMLAnchorElement = <HTMLAnchorElement>document.getElementById('downloadFileLink');
fileInput.href = url;
fileInput.setAttribute('download', path.fileName);
fileInput.click();
})
.catch((error)=> {
this.errorService.showError('Impossible de télécharger le fichier', error);
});
}
}

这是安卓部分:

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DownloadManager;
import android.app.DownloadManager.Request;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuInflater;
import android.webkit.DownloadListener;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceError;
import android.webkit.WebResourceRequest;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;

public class BlablaActivity extends Activity {

private WebView webView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_xyz_mobile);

webView = (WebView) findViewById(R.id.webView);

WebSettings webSettings = webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setDomStorageEnabled(true);
webSettings.setDatabaseEnabled(true);

WebChromeClient webChromeClient = new WebChromeClient() {
};
webView.setWebChromeClient(webChromeClient);

final DownloadListener downloadListener = new DownloadListener() {
public void onDownloadStart(String url, String userAgent,
String contentDisposition, String mimetype,
long contentLength) {
Request request = new Request(Uri.parse(url));
request.allowScanningByMediaScanner();
request.setNotificationVisibility(Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, "download");

DownloadManager downloadManager = (DownloadManager) getSystemService(DOWNLOAD_SERVICE);
downloadManager.enqueue(request);

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); //This is important!
intent.addCategory(Intent.CATEGORY_OPENABLE); //CATEGORY.OPENABLE
intent.setType("*/*");//any application,any extension
Toast.makeText(getApplicationContext(), "Téléchargement", //To notify the Client that the file is being downloaded
Toast.LENGTH_LONG).show();
}
};
webView.setDownloadListener(downloadListener);

WebViewClient webViewClient = new WebViewClient() {
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}

@Override
public void onLoadResource(WebView view, String url) {
super.onLoadResource(view, url);
}

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
webView.setDownloadListener(downloadListener);
return true;
}

@Override
public void onReceivedError(WebView view, WebResourceRequest request, WebResourceError error) {
// view.loadUrl("file:///android_asset/load_error.html");
// view.loadData("Erreur : " + error + ". Veuillez vous connecter pour utiliser l'application.", "text/plain", "utf-8");
showDefaultError();
}
};
webView.setWebViewClient(webViewClient);

loadJsApp();
}

private void loadJsApp() {
webView.loadUrl("https://www.mymobileapp.com");
}

private void showDefaultError() {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Erreur");
builder.setMessage("Désolé, une erreur s'est produite. Veuillez vérifier votre connexion Internet.");

builder.setIcon(android.R.drawable.ic_dialog_alert);

AlertDialog dialog = builder.create();
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
loadJsApp();
}
});

dialog.show();

//Toast.makeText(this, "Réseau indisponible !", Toast.LENGTH_LONG).show();
}

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

}

最佳答案

奇怪的是,HTML anchor 元素上的“下载”属性在 Android WebView 中似乎没有得到正确处理。删除该属性应该会按预期触发文件下载。

关于android - 为什么我的下载链接在 Android WebView 中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34648515/

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