上下文
- 我的应用程序使用webview显示包含一些数据的报告
- 此webview顶部有一个带有打印按钮的工具栏
- 单击时,打印按钮会打开一个菜单,让用户从可打印报告的应用程序列表中进行选择
- 打印过程:获取报告的屏幕截图,压缩报告以减小其大小,将png发送到打印机应用程序。
问题:
- 当打印机应用程序打开并打印图像时,我收到一个ANR,说我的后台应用程序停止工作
- 如果我按下android上的后退按钮,应用程序仍处于活动状态,我可以在没有任何错误的情况下导航,这很奇怪,我刚刚收到警报,它已停止工作。
一些启发代码:
截屏并发送到打印机应用的代码
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.print) {
Bitmap bitmap = Util.screenShot(mWebView);
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
File file = new File(Environment.getExternalStorageDirectory() + File.separator + "screenshot.png");
try {
file.createNewFile();
FileOutputStream fo = new FileOutputStream(file);
fo.write(bytes.toByteArray());
fo.close();
} catch (IOException e) {
e.printStackTrace();
}
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/png");
intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file));
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
日志:
03-23 14:32:54.726 11347-11388/br.gov.go.agrodefesa.tabletagrodefesa E/IMGSRV: kickresource.c:1130: Debug assertion failed!
03-23 14:32:54.836 9039-9039/? A/google-breakpad: M B0DE6000 00018000 00015000 000000000000000000000000000000000 data@app@br.gov.go.agrodefesa.tabletagrodefesa-1@base.apk@classes.dex
03-23 14:32:54.876 11347-11388/br.gov.go.agrodefesa.tabletagrodefesa W/google-breakpad: ### ### ### ### ### ### ### ### ### ### ### ### ###
03-23 14:32:54.876 11347-11388/br.gov.go.agrodefesa.tabletagrodefesa W/google-breakpad: Chrome build fingerprint:
03-23 14:32:54.876 11347-11388/br.gov.go.agrodefesa.tabletagrodefesa W/google-breakpad: 1.27
03-23 14:32:54.876 11347-11388/br.gov.go.agrodefesa.tabletagrodefesa W/google-breakpad: 1
03-23 14:32:54.876 11347-11388/br.gov.go.agrodefesa.tabletagrodefesa W/google-breakpad: ### ### ### ### ### ### ### ### ### ### ### ### ###
03-23 14:32:54.876 11347-11388/br.gov.go.agrodefesa.tabletagrodefesa A/libc: Fatal signal 6 (SIGABRT), code -6 in tid 11388 (RenderThread)
03-23 14:32:54.976 9769-9769/? I/DEBUG: pid: 11347, tid: 11388, name: RenderThread >>> br.gov.go.agrodefesa.tabletagrodefesa <<<
03-23 14:32:55.056 1710-1777/? D/UsageStatistics: Pkg: br.gov.go.agrodefesa.tabletagrodefesa ForegroundTime: 2701177 FirstTime: 3-22-2017 21:00:00:000 LastTime: 3-23-2017 14:32:54:057 LastTimeUsed: 3-23-2017 14:32:54:057
03-23 14:32:55.466 524-1117/? I/WindowState: WIN DEATH: Window{2ba92055 u0 br.gov.go.agrodefesa.tabletagrodefesa/br.gov.go.agrodefesa.tabletagrodefesa.activities.TermoFiscalizacaoListActivity}
03-23 14:32:55.476 524-980/? I/WindowState: WIN DEATH: Window{1892d68d u0 br.gov.go.agrodefesa.tabletagrodefesa/br.gov.go.agrodefesa.tabletagrodefesa.activities.ImpressaoActivity}
03-23 14:32:55.496 524-654/? I/ActivityManager: Process br.gov.go.agrodefesa.tabletagrodefesa (pid 11347) has died
答案 0 :(得分:0)
哦,我看到了你的错误。 你正在使用这个:
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
在CompressFormat中生成JPEG图像 并在意图中发送PNG图像
intent.setAction(Intent.ACTION_SEND);
intent.setType("image/png");
这是错误的原因。为了解决这个问题,请使用PNG而不是JPEG。