我正在开发一个功能,用户可以在其中创建所执行工作订单的报告。通过此报告,您将获得订单本身的信息,例如客户名称,服务,位置等...以及捕获的服务图像。我已经设法使用我想要的数据(包括格式(生成报告。但是,我很难在同一报告中附加从相机或图库中获取的图像。已经研究了很多次,我所做的尝试没有出现在报告中的图像。
从相机获取图像
private val pathImage: ArrayList<String> = arrayListOf<String>()
override fun onActivityResult(
requestCode: Int,
resultCode: Int,
data: Intent?
) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == CAM && resultCode == Activity.RESULT_OK && data != null) {
val novaIntent = Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, uri)
sendBroadcast(novaIntent)
imageCam = uri.getPath().toString()
if (imageCam != null) {
Log.i("TAG", "Camera: " + imageCam)
pathImage.add(imageCam)
}
} else if (requestCode == GALLERY && resultCode == Activity.RESULT_OK && data != null) {
val clipData = data.clipData
if (clipData != null) {
for (i in 0 until clipData.itemCount) {
val uri: Uri = clipData.getItemAt(i).uri
pathImage.add(uri.path.toString())
}
} else {
val uri = data.data
if (uri != null) {
pathImage.add(uri.path.toString())
}
}
}
}
创建报表
val mDoc = Document()
//pdf file name
mFileName = SimpleDateFormat(
"ddMMyyyy_HHmmss",
Locale.getDefault()
).format(System.currentTimeMillis())
//pdf file path
mFilePath =
Environment.getExternalStorageDirectory().toString() + "/" + mFileName + ".pdf"
try {
//create instance of PdfWriter class
PdfWriter.getInstance(mDoc, FileOutputStream(mFilePath))
//open the document for writing
mDoc.open()
//settings
mDoc.setPageSize(PageSize.A4)
mDoc.addCreationDate()
mDoc.addAuthor(userDTO.user.fullname)
mDoc.addCreator("Pratik Butani")
mDoc.setPageSize(PageSize.LETTER)
//font settings
val mColorAccent = BaseColor(0, 153, 204, 255)
val mHeadingFontSize = 20.0f
val mValueFontSize = 16.0f
//Font
val fontName = BaseFont.createFont(
"assets/fonts/brandon_medium.otf",
BaseFont.IDENTITY_H,
BaseFont.EMBEDDED,
true
)
for ( image in pathImage) {
val img = Image.getInstance(image)
mDoc.setPageSize(img)
mDoc.newPage()
img.setAbsolutePosition(0f, 0f)
mDoc.add(img)
}
//close document
mDoc.close()
} catch (e: Exception) {
//if anything goes wrong causing exception, get and show exception message
Toast.makeText(this@DetailsActivity, e.message, Toast.LENGTH_SHORT).show()
}
返回错误:/文档/图像:8495:打开失败:ENOENT(没有此类文件或目录(
您在此行pathImage.add(uri.path.toString())
onActivityResult
方法中从uri 获取路径时犯了错误。
您可以尝试以下方法从 URI 获取路径:
public static String getPathFromUri(final Context context, final Uri uri) {
// DocumentProvider
if (DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider
if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("primary".equalsIgnoreCase(type)) {
return Environment.getExternalStorageDirectory() + "/" + split[1];
}
// TODO handle non-primary volumes
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
Uri contentUri = null;
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[]{
split[1]
};
return getDataColumn(context, contentUri, selection, selectionArgs);
}
}
// MediaStore (and general)
else if ("content".equalsIgnoreCase(uri.getScheme())) {
// Return the remote address
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();
return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
return null;
}
这是对您有所帮助的完整要点。
谢谢。