【问题标题】:Saving activity into pdf file将活动保存到 pdf 文件中
【发布时间】:2016-05-05 08:51:32
【问题描述】:

我需要将活动内容保存到 .pdf 文件中。类似截图的东西。我可以使用以下代码将活动的单个视图保存到文件中:

PdfDocument document = new PdfDocument();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(600,300, 1).create();
PdfDocument.Page page = document.startPage(pageInfo);
View content = findViewById(R.id.editText1);
content.draw(page.getCanvas());
document.finishPage(page);

但是如何保存(打印屏幕)整个活动,而不仅仅是一个视图?

谢谢。

【问题讨论】:

    标签: android pdf pdf-generation


    【解决方案1】:

    您可以使用给定的代码截取当前活动的屏幕截图

    public void saveBitmap(Bitmap bitmap) {
    File imagePath = new File(Environment.getExternalStorageDirectory() + "/screenshot.png");
    FileOutputStream fos;
    try {
        fos = new FileOutputStream(imagePath);
        bitmap.compress(CompressFormat.JPEG, 100, fos);
        fos.flush();
        fos.close();
    } catch (FileNotFoundException e) {
        Log.e("GREC", e.getMessage(), e);
    } catch (IOException e) {
        Log.e("GREC", e.getMessage(), e);
    }
    

    }

    并确保在清单中授予存储的写入权限

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

    【讨论】:

    • OP 明确要求将其保存为 PDF,而不是 JPEG。
    猜你喜欢
    • 2016-12-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-04
    • 1970-01-01
    • 1970-01-01
    • 2019-10-30
    • 2019-04-09
    相关资源
    最近更新 更多