andob / android-awt

Combination of code from Apache Harmony and Apache Commons Imaging to replace classes in java.awt for Android.
Apache License 2.0
28 stars 15 forks source link

issue in add image to pdf in android #8

Closed shambhu-sharan closed 3 years ago

shambhu-sharan commented 3 years ago

image not showing in pdf .

andob commented 3 years ago

ok. can you give me example code and image?

shambhu-sharan commented 3 years ago

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);

images (1)

andob commented 3 years ago

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
shambhu-sharan commented 3 years ago

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 .

andob commented 3 years ago

ok, I'll take a look later today.

andob commented 3 years ago

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)