我已在我的应用中为图像实现了下载模块。他们在WebView
。虽然,我有一个奇怪的错误,当我在模拟器上运行应用程序时,没有调用方法onDownloadStart()
。我在我的设备中使用了相同的API(22),但它在那里工作得很好。怎么回事?有任何想法吗?这就是我将下载监听器附加到WebView
的方式:
ExportModule exportModule = new ExportModule(activity.getBaseContext(), activity, this.webView);
this.webView.addJavascriptInterface(exportModule, "jsint");
this.webView.setDownloadListener(exportModule);
来自@JavascriptInterface
的代码是从它运行的,但后来onDownloadSart()
未在设备中调用。来自onDownloadStart()
班级的ExportModule
:
@Override
public void onDownloadStart(String url, String userAgent, String contentDisposition, String mimetype, long contentLength) {
System.out.println("IM IN onDwoanloadStart");
String intentType = "image/png";
String fileName = "img.png";
try {
if(url != null) {
FileOutputStream fos;
fos = context.openFileOutput(fileName, Context.MODE_PRIVATE);
//conversion stuff
fos.write(decodedStr);
fos.getFD().sync();
fos.flush();
fos.close();
Intent sendIntent = new Intent();
sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
sendIntent.setAction(Intent.ACTION_SEND);
File filePath = new File(String.valueOf(context.getFilesDir()));
File newFile = new File(filePath, fileName);
sendIntent.putExtra(Intent.EXTRA_STREAM, FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".FileProvider", newFile));
sendIntent.setType(intentType);
context.startActivity(sendIntent);
}
} catch (IOException e) {
e.printStackTrace();
}
}