Closed shambhu-sharan closed 3 years ago
ok. can you give me example code and image?
in gradle i have added
implementation 'com.github.librepdf:openpdf:1.3.25' implementation 'ro.andob.androidawt:androidawt:1.0.4'
file path-->file:///storage/emulated/0/pdf/company_logo_0.jpg
Bitmap bitmap = BitmapFactory.decodeFile(file.getPath()); ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); Image image = Image.getInstance(stream.toByteArray()); image.scaleAbsolute(100, 100); PdfPCell cellImg = new PdfPCell(image, true); PdfPTable imgTable = new PdfPTable(1); imgTable.addCell(cellImg);
I see that you are accesing the file path on non-scoped storage: file:///storage/emulated/0/company_logo_0.jpg non-scoped storage and write_external storage permissions are deprecated in latest Android versions and not a good practice. is that ok??
have you tried to access it from /storage/emulated/0/Android/your_package_name/files/company_logo_0.jpg or /data/data/your_package_name/files/pdf/company_logo_0.jpg
you can get those paths like this:
File dir = context.getExternalFilesDir(null);
File file = new File(dir, "company_logo_0.jpg");
///storage/emulated/0/Android/your_package_name/files/company_logo_0.jpg
or
File file = context.getFileStreamPath("company_logo_0.jpg");
///data/data/your_package_name/files/pdf/company_logo_0.jpg
I have targeted api v 29 (scoped storage come on 29+) and I have checked , I am getting file from storage, conversion to bitmap also ok . some png image display in pdf but most of image doesn't .
ok, I'll take a look later today.
Image.getInstance(bytes)
doesn't seem to work, but it works if you do Image.getInstance(filePath)
Try converting it into JPEG first:
//convert to jpeg
val bitmap=BitmapFactory.decodeFile(imageFile.absolutePath)
FileOutputStream(imageFile).use { outputStream ->
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream)
}
val table=PdfPTable(1)
val image=Image.getInstance(imageFile.absolutePath)
image.scaleAbsolute(100.0f, 100.0f)
val cell=PdfPCell(image, true)
table.addCell(cell)
pdf.add(table)
image not showing in pdf .