3

Context:
- My app uses a webview to display a report with some data.
- This webview has a toolbar on top with a print button.
- When clicked, the print button open up a menu that lets the user choose from a list of apps that can print the report.
- The process of printing: Take a screenshot of the report, compress it to reduce its size, send the png to the printer app.

Problem:
- When the printer app open up with the image to print, i receive an ANR saying that my background app stopped working.
- If i press the back button on the android, the application is still alive, and i can navigate without any errors, which is odd, i just received an alert that it has stopped working.

Some code for enlightment:

Code that takes screenshot and send to printer app

@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);
}

The log:

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
MurifoX
  • 14,991
  • 3
  • 36
  • 60
  • I tried your code and the code works fine for me. I think you should try allowing your app to run in background. – HarshitMadhav Mar 23 '17 at 17:35
  • @HarshitAgrawal What do u mean by "allow the app to run in the background"? Encapsulate the send code in a Service? – MurifoX Mar 23 '17 at 17:38
  • I mean sometimes there are issues related to device they dont allow many permissions until the device is root. I mean to say that just force your app in the device to run in background. – HarshitMadhav Mar 23 '17 at 17:40
  • @Onik There are several activities stacked when it came to this point. Login -> Dashboard -> Search -> Print. I don't know the specific amount of time. – MurifoX Mar 23 '17 at 20:20
  • @Onik I'll try to take the bitmap processing off the main thread and see how it goes. – MurifoX Mar 23 '17 at 20:35
  • @Onik It is pretty fast, the whole process calculating with `currentTimeMillis` is returning 80. Also, i've tried moving the code to an `AsyncTask` and the ANR keeps appearing. – MurifoX Mar 23 '17 at 20:46

1 Answers1

0

Oh I see your error. You are using this :

bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

which is generating a JPEG image in CompressFormat and in the Intent you are sending the PNG image

intent.setAction(Intent.ACTION_SEND);
intent.setType("image/png");

which is the cause of error. In order to resolve this use PNG instead of JPEG.

HarshitMadhav
  • 4,769
  • 6
  • 36
  • 45