0

I have save jpg file to internal storage code given below;

  Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File folderPath = new File(Environment.getExternalStorageDirectory() + "/OCR");

    if (!folderPath.exists()) {
        if (folderPath.mkdir()) ; //directory is created;
    } else {
        File photo = new File(folderPath, "OCRPIC" + UUID.randomUUID().toString().substring(0, 5) + ".jpg");
        imageUri = FileProvider.getUriForFile(MainActivity.this,
                BuildConfig.APPLICATION_ID + ".provider", photo);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
        startActivityForResult(intent, PHOTO_REQUEST);
    }

Now I wanted jpg image to covert and show in pdf reader. I have tried some code given below but throws errors says not supported file formate.

 private void viewPdf(File file) {
    Intent intent;
    intent = new Intent(Intent.ACTION_VIEW);


    Uri photoURI = FileProvider.getUriForFile(MyFileActivity.this, BuildConfig.APPLICATION_ID + ".provider", file);
    MyFileActivity.this.grantUriPermission("com.android.camera", photoURI,
            Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);


    intent.setDataAndType(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N ?
            android.support.v4.content.FileProvider.getUriForFile(MyFileActivity.this, BuildConfig.APPLICATION_ID + ".provider", file) : Uri.fromFile(file), "application/pdf").addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
    try {
        startActivity(intent);
    } catch (ActivityNotFoundException e) {
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("No Application Found");
        builder.setMessage("Download one from Android Market?");
        builder.setPositiveButton("Yes, Please",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent marketIntent = new Intent(Intent.ACTION_VIEW);
                        marketIntent.setData(Uri.parse("market://details?id=com.adobe.reader"));
                        startActivity(marketIntent);
                    }
                });
        builder.setNegativeButton("No, Thanks", null);
        builder.create().show();
    }
}

Any suggestion or sample other than itext libray to solve this conversion.

  • 1
    https://stackoverflow.com/questions/36305362/how-to-convert-image-to-pdf , please check the following link, it may help you. – Rajshree Tiwari Jul 24 '18 at 10:30
  • you are not converting the image to pdf. your code is trying to open an Image in a pdf reader. Itext is a good library to make or read pdf's in android. any specific reason not to use that ? – Vihaari Varma Jul 24 '18 at 11:39
  • @ Vihaari Varma Itext lib license is tricky says my app should be released for free of cost to get lib for free but I may have problem in releasing app as paid in future.so I wish to avoid itext. –  Jul 25 '18 at 01:50

0 Answers0