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.
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
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..
回答2:
Since I'm not able to comment on bakriOnFire's answer, I have to write an Answer to the topic.
thank you for the solution. bttw what is this line of code doing b.compress(Bitmap.CompressFormat.PNG, 100, stream); and what is b? – chai May 8 '13 at 11:20
the linle of code should be:
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
The Bitmap is getting compressed using PNG encoding and written to an ByteArrayOutputStream. This is required since Image.getInstance() just can handle ByteArrays.