java - 我想在 Android Studio 中将文件类型转换为 PdfDocument 类型
问题描述
我试过代码
@Override
protected void onActivityResult(int requestCode, int resultCode,
@Nullable Intent data)
{
switch (requestCode)
{
case 10:
if (resultCode==RESULT_OK)
{
String path = data.getData().getPath();
path_disp.setText(path);
File file = new File(path);
PdfDocument pdf = new PdfDocument(file.getAbsolutePath(), null);
}
break;
}
}
但这给了我一个错误“PdfDocument()中的 PdfDocument()不能应用于(java.lang.String,null)”
解决方案
https://developer.android.com/reference/android/graphics/pdf/PdfDocument
看起来类 PdfDocument 没有可以接受字符串的构造函数。
// create a new document
PdfDocument document = new PdfDocument();
// crate a page description
PageInfo pageInfo = new PageInfo.Builder(new Rect(0, 0, 100, 100), 1).create();
// start a page
Page page = document.startPage(pageInfo);
// draw something on the page
View content = getContentView();
content.draw(page.getCanvas());
// finish the page
document.finishPage(page);
. . .
// add more pages
. . .
// write the document content
document.writeTo(getOutputStream());
// close the document
document.close();
您必须先创建一个页面,然后才能在画布上绘制一些东西。在你的情况下,它会是
yourCanvas.drawText("Your string.")
推荐阅读
- machine-learning - 在算法 LambdaRank(在学习排名中)|∆ NDCG| 是什么?方法?
- javascript - 无法使用 multer 读取未定义的属性“路径”
- c++ - 尝试制作哈希表,无法映射所有键,还程序崩溃
- apache - 使用 htaccess 删除部分 URL
- javascript - 使用 map 映射到 rxjs 中的属性
- php - 访问页面时间隔后调用函数
- tcl - 如何使用 tcl 读取和执行定点值的计算
- git - 带参数的 Git 别名
- wso2 - WSO2 API Manager - 使用动态端点进行负载平衡
- html - Ionic 按钮内的图像