I am using iText lib to insert text into a pdf file. I want to insert pictures into the same pdf as well, so I created an activity specially for taking pictures (PhotoActivity). It happened to me that the user might need to insert not only one, but multiple pictures, and the code below is kind of static, it is effective in case the user takes only one picture:
import java.io.*;
import com.lowagie.text.*;
import com.lowagie.text.pdf.*;
public class imagesPDF
{
public static void main(String arg[])throws Exception
{
Document document=new Document();
PdfWriter.getInstance(document,new FileOutputStream("pdfFile.pdf"));
document.open();
Image image = Image.getInstance ("TakenPhoto.jpg");
document.add(new Paragraph("Image Heading"));
document.add(image);
document.close();
}
}
My PhotoActivity code (called by intent from the MainActivity) is as it follows:
import android.content.Intent;
import android.graphics.Bitmap;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class PhotoActivity extends ActionBarActivity {
Button b1;
ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_photo);
b1 = (Button) findViewById(R.id.button1);
iv = (ImageView) findViewById(R.id.imageView);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
}
});
}
// Button to go back to the MainActivity
public void onclickButton2(View view) {
PhotoActivity.this.finish();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
Bitmap bp = (Bitmap) data.getExtras().get("data");
iv.setImageBitmap(bp);
}
@Override
protected void onDestroy() {
super.onDestroy();
}
I would like to know if someone could show me a dynamic way to programmatically acknowledge the # of pictures taken during the execution of the PhotoActivity, and once knowing this, put them into the pdf. I appreciate if anyone could help me. Thank you in advance.