简体   繁体   中英

How to create PDF file from bitmap image in Android

I am trying to convert bitmap image to PDF file. I am selecting image from gallery and want to convert that image file into pdf document. I am trying to do so but I am getting empty PDF file.

Below is my code:

public class Convert extends AppCompatActivity {

private static final int REQ_CODE = 99;
Bitmap bitmap = null;
ImageView image;
Button convert;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_convert);

    image = findViewById(R.id.image);
    convert = findViewById(R.id.convert);

    openGallery();

    convert.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            // create a new document
            PdfDocument document = new PdfDocument();

            // create a page description
            PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(100, 100, 1).create();

            // start a page
            PdfDocument.Page page = document.startPage(pageInfo);

            // Draw the bitmap onto the page
            Canvas canvas = new Canvas();
            canvas.drawBitmap(bitmap, 0f, 0f, null);
            document.finishPage(page);

            String path = android.os.Environment.getExternalStorageDirectory().toString();
            try {
                document.writeTo(new FileOutputStream(path + "/example.pdf"));
            } catch (IOException e) {
                e.printStackTrace();
            }
            document.close();
        }
    });
}

private void openGallery(){

    int pref = ScanConstants.OPEN_MEDIA;
    Intent in = new Intent(Convert.this, ScanActivity.class);
    in.putExtra(ScanConstants.OPEN_INTENT_PREFERENCE,pref);
    startActivityForResult(in,REQ_CODE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if(requestCode == REQ_CODE && resultCode == Activity.RESULT_OK){

       Uri uri = data.getExtras().getParcelable(ScanConstants.SCANNED_RESULT);
       try{
           bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),uri);
           if(uri != null){
               getContentResolver().delete(uri,null,null);
               image.setImageBitmap(bitmap);
           }
       } catch (FileNotFoundException e) {
           e.printStackTrace();
       } catch (IOException e) {
           e.printStackTrace();
       }
     }
  }
} 

What am I doing wrong?

I think you are not writing to PdfDocument.Page canvas object so this is what you should try,

// start a page
PdfDocument.Page page = document.startPage(pageInfo);

// Draw the bitmap onto the page
Canvas canvas = page.getCanvas();
canvas.drawBitmap(bitmap, 0f, 0f, null);
document.finishPage(page);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM