将位图中的 PDF 保存到内部存储或作为意图发送
Save a PDF from bitmap into internal store or send as intent
我想从我的位图生成 PDF,但我不想使用外部存储(在所有示例中如何使用外部存储,但我不想强迫用户拥有 SD 卡在 phone)。我想通过意图共享我的 PDF,通过电子邮件发送或存储在 GoogleDrive/Phone 存储中。我正在使用 Itext library。
目前,我无法将其保存到内部存储(尝试保存时我有一个 "Error opening file: src" Toast ),而且 google 驱动器和邮件发送也失败(收到消息文件不能是 upload/attached)
代码:
Bitmap bitmap = loadBitmapFromView(view);
//Now create the name of your PDF file that you will generate
File pdfFile = new File(CreatePDFActivity.this.getFilesDir(), "myPdfFile.pdf");
try {
Document document=new Document();
PdfWriter.getInstance(document,new FileOutputStream(pdfFile.getAbsolutePath())); // Change pdf's name.
document.open();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
Image img =Image.getInstance(stream.toByteArray()); // Change image's name and extension.
float scaler = ((document.getPageSize().getWidth() - document.leftMargin()
- document.rightMargin() - 0) / img.getWidth()) * 100; // 0 means you have no indentation. If you have any, change it.
img.scalePercent(scaler);
img.setAlignment(Image.ALIGN_CENTER | Image.ALIGN_TOP);
document.add(img);
document.close();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_SUBJECT, "Test ");
Uri uri = Uri.fromFile(new File( CreatePDFActivity.this.getFilesDir(), "myPdfFile.pdf"));
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.addFlags( Intent.FLAG_GRANT_READ_URI_PERMISSION );
intent.setType("application/pdf");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
catch (Exception e){
e.printStackTrace();
}
in all examples how to do it they using external storage, but I dont want force users to have a SD card in phone
External storage is not removable storage。在当今使用的 Android 设备中,约 99% 的外部存储不是 SD 卡。
Ive got a "Error opening file: src" Toast when try to save
你问题的代码中没有Toast
。
the google drive and mail sending also failed (got message that file cannot be upload/attached)
Third-party 应用无法访问您应用的 internal storage 部分。此外,一旦您将 targetSdkVersion
提高到 24 或更高,您现有的代码将在 Android 7.0+ 设备上崩溃。使用 FileProvider
使 third-party 个应用程序可以使用内部存储中保存的文件。
我想从我的位图生成 PDF,但我不想使用外部存储(在所有示例中如何使用外部存储,但我不想强迫用户拥有 SD 卡在 phone)。我想通过意图共享我的 PDF,通过电子邮件发送或存储在 GoogleDrive/Phone 存储中。我正在使用 Itext library。
目前,我无法将其保存到内部存储(尝试保存时我有一个 "Error opening file: src" Toast ),而且 google 驱动器和邮件发送也失败(收到消息文件不能是 upload/attached)
代码:
Bitmap bitmap = loadBitmapFromView(view);
//Now create the name of your PDF file that you will generate
File pdfFile = new File(CreatePDFActivity.this.getFilesDir(), "myPdfFile.pdf");
try {
Document document=new Document();
PdfWriter.getInstance(document,new FileOutputStream(pdfFile.getAbsolutePath())); // Change pdf's name.
document.open();
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
Image img =Image.getInstance(stream.toByteArray()); // Change image's name and extension.
float scaler = ((document.getPageSize().getWidth() - document.leftMargin()
- document.rightMargin() - 0) / img.getWidth()) * 100; // 0 means you have no indentation. If you have any, change it.
img.scalePercent(scaler);
img.setAlignment(Image.ALIGN_CENTER | Image.ALIGN_TOP);
document.add(img);
document.close();
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_SUBJECT, "Test ");
Uri uri = Uri.fromFile(new File( CreatePDFActivity.this.getFilesDir(), "myPdfFile.pdf"));
intent.putExtra(Intent.EXTRA_STREAM, uri);
intent.addFlags( Intent.FLAG_GRANT_READ_URI_PERMISSION );
intent.setType("application/pdf");
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intent);
}
catch (Exception e){
e.printStackTrace();
}
in all examples how to do it they using external storage, but I dont want force users to have a SD card in phone
External storage is not removable storage。在当今使用的 Android 设备中,约 99% 的外部存储不是 SD 卡。
Ive got a "Error opening file: src" Toast when try to save
你问题的代码中没有Toast
。
the google drive and mail sending also failed (got message that file cannot be upload/attached)
Third-party 应用无法访问您应用的 internal storage 部分。此外,一旦您将 targetSdkVersion
提高到 24 或更高,您现有的代码将在 Android 7.0+ 设备上崩溃。使用 FileProvider
使 third-party 个应用程序可以使用内部存储中保存的文件。