Issue
Can anyone please show me how to use iText API in android to convert images captured which is already in gallery and save it as pdf document. Help needed as soon as possible. Main objective is to create android application whereby able to get multiple images from the gallery and save it as pdf format.
Solution
To get the images from gallery u'll have to start the startActivityForResult and in onActivityResult u can store the image in the pdf file:-
First call the gallery intent as:-
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
Then in onActivityResult get the bitmap and write it in the PDF
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
switch(requestCode){
case SELECT_PICTURE:
Uri selectedImageUri = data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };
Cursor cursor = getContentResolver().query(selectedImageUri,filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
Bitmap bmp = BitmapFactory.decodeFile(picturePath);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
Document document = new Document();
File f=new File(Environment.getExternalStorageDirectory(), "SimpleImages.pdf");
PdfWriter.getInstance(document,new FileOutputStream(f));
document.open();
document.add(new Paragraph("Simple Image"));
Image image = Image.getInstance(stream.toByteArray());
document.add(image);
document.close();
break;
}
}
}
Hope this helps..
Answered By - bakriOnFire
Answer Checked By - David Marino (JavaFixing Volunteer)