How to convert JPG image file into PDF file in your Android App? - complete source code.

Поділитися
Вставка
  • Опубліковано 20 вер 2024
  • This video shows the steps to develop your App in Android Studio environment to convert the JPG file into PDF file format. The steps are very simple. In this App it is shown that it first reads the JPG file in your App and then converts it into Bitmap. Subsequently it creates a PDF file and then draws the bitmap image in it's page canvas. And finally saves the pdf document to the specified PDF file.
    The JPG image/ picture file used in this video has been created using myCamera App. The steps to create it is shown in the below UA-cam video and is recommended if interested to know the steps to create the jpg file:
    • How to create camera A...
    We will be glad to hear from you regarding any query, suggestions or appreciations at: programmerworld1990@gmail.com
    programmerworl...
    Source Code:
    package com.example.myjpgtopdf;
    import android.content.pm.PackageManager;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.graphics.pdf.PdfDocument;
    import android.os.Bundle;
    import android.os.Environment;
    import android.view.View;
    import androidx.appcompat.app.AppCompatActivity;
    import androidx.core.app.ActivityCompat;
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import static android.Manifest.permission.WRITE_EXTERNAL_STORAGE;
    public class MainActivity extends AppCompatActivity {
    private String directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + "/myCamera/";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ActivityCompat.requestPermissions(this, new String[]{WRITE_EXTERNAL_STORAGE}, PackageManager.PERMISSION_GRANTED);
    }
    public void convertButton(View view){
    String file = directory + "3.jpg";
    Bitmap bitmap = BitmapFactory.decodeFile(file);
    PdfDocument pdfDocument = new PdfDocument();
    PdfDocument.PageInfo myPageInfo = new PdfDocument.PageInfo.Builder(960,1280,1).create();
    PdfDocument.Page page = pdfDocument.startPage(myPageInfo);
    page.getCanvas().drawBitmap(bitmap,0,0, null);
    pdfDocument.finishPage(page);
    String pdfFile = directory + "/myPDFFile_3.pdf";
    File myPDFFile = new File(pdfFile);
    try {
    pdfDocument.writeTo(new FileOutputStream(myPDFFile));
    } catch (IOException e) {
    e.printStackTrace();
    }
    pdfDocument.close();
    }
    }

КОМЕНТАРІ • 41

  • @real33479
    @real33479 3 роки тому

    Brother, thank you for the explanation
    I did everything, but when I reduce the size of the image it loses clarity and becomes blurry
    Is there a way to clarify the picture?

  • @tamirmoav3774
    @tamirmoav3774 2 роки тому

    sorry but my bitmap is null , and where did the getCanvas came from? the galley does not open

    • @ProgrammerWorld
      @ProgrammerWorld  2 роки тому

      In this code, the file name is hardcoded in below line. Later the image file is converted into bitmap.
      String file = directory + “3.jpg”;
      Bitmap bitmap = BitmapFactory.decodeFile(file);
      So, if you are directing to the right file then it can be changed to bitmap using the above concept.
      For reference, the complete source code shown in this video is also shared in the below link:
      programmerworld.co/android/how-to-convert-jpg-image-file-into-pdf-file-in-your-android-app-complete-source-code/
      Cheers
      Programmer World
      programmerworld.co
      -

  • @dhruvguru4506
    @dhruvguru4506 Рік тому

    How to convert multiple image in PDF file pleases give this solution

    • @ProgrammerWorld
      @ProgrammerWorld  Рік тому

      You should create multiple Bitmaps using the below line of code:
      Bitmap bitmap = BitmapFactory.decodeFile(file);
      And then add one by one all the bitmaps in the pdf page in the below code:
      page.getCanvas().drawBitmap(bitmap,0,0, null);
      To create multiple bitmaps, one such use case is discussed in the below page:
      programmerworld.co/android/how-to-create-a-gif-from-jpeg-images-in-your-android-app-android-studio-complete-code/
      Cheers
      Programmer World
      programmerworld.co
      -

  • @mohdasim9974
    @mohdasim9974 3 роки тому

    How to add an image with hyperlink in pdf

  • @real33479
    @real33479 3 роки тому

    Can you choose any picture without writing its name
    Can I add a number of texts with the image?

    • @ProgrammerWorld
      @ProgrammerWorld  3 роки тому

      Can you choose any picture without writing its name?
      =>
      Yes, you can choose any picture available on your device. However, you will have to give the complete file name of that picture/ image. In my code, it is given using the below lines of code:
      String file = directory + “3.jpg”;
      Bitmap bitmap = BitmapFactory.decodeFile(file);
      Refer to the below link for complete source code:
      programmerworld.co/android/how-to-convert-jpg-image-file-into-pdf-file-in-your-android-app-complete-source-code/
      Can I add a number of texts with the image?
      =>
      Yes, I think you can add any content in your PDF including numbers and texts. You can add multiple pages if required. You can refer to the below link which shows steps to add texts:
      programmerworld.co/android/how-to-create-pdf-file-in-your-android-app-complete-source-code-using-android-studio/
      Good Luck
      Programmer World
      programmerworld.co
      -

  • @evanspr1403
    @evanspr1403 4 роки тому

    Hello, is there a way to do this, but with an image that is selected from the gallery and not previously loaded on a route?

    • @ProgrammerWorld
      @ProgrammerWorld  4 роки тому +2

      I think that's what is shown in this video. In the below file name (directory path) you have to just change it to your desired path (gallery in your case).
      private String directory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) + “/myCamera/”;
      String file = directory + “3.jpg”;
      The source code is available at:
      programmerworld.co/android/how-to-convert-jpg-image-file-into-pdf-file-in-your-android-app-complete-source-code/
      Good Luck
      Programmer World
      programmerworld.co
      -

  • @sachinrakibe7532
    @sachinrakibe7532 4 роки тому +1

    sir please make video on JPG to video or mp4

    • @ProgrammerWorld
      @ProgrammerWorld  4 роки тому

      Thanks for the suggestion. I will try to make one on this topic.
      However, if interested one can refer to my below video which shows how to create a custom video player App:
      ua-cam.com/video/ZJlUfhJQYiE/v-deo.html
      Cheers
      Programmer World
      programmerworld.co
      -

  • @daniyalhussain7904
    @daniyalhussain7904 4 роки тому +1

    Thanks man means alot

  • @reubenkurian1155
    @reubenkurian1155 4 роки тому

    wrote the exact steps but the app is crashing as soon as i press the button
    it says "unfortunately has stopped working" ...any solution?

    • @ProgrammerWorld
      @ProgrammerWorld  4 роки тому

      Is it crashing in the Emulator also? If yes then good. Just debug your code. In case required, refer to my below video for debugging steps:
      ua-cam.com/video/Y_y2jR1GSC8/v-deo.html
      If debugging is not possible then please put try-catch in the onCreate method. It will prevent the App from crashing.
      Also, the complete source code of this video is shared in the below link. If possible kindly check it out.
      programmerworld.co/android/how-to-convert-jpg-image-file-into-pdf-file-in-your-android-app-complete-source-code/
      Cheers
      Programmer World
      programmerworld.co
      -

    • @reubenkurian1155
      @reubenkurian1155 4 роки тому

      ​@@ProgrammerWorld can you please tell how to add the try-catch in oncreate. I mean when i put that part in on create 'pdfDocument' shows red and asks if to create local variable?
      what do i do?

    • @reubenkurian1155
      @reubenkurian1155 4 роки тому

      all permissions are also given ..still its crashing

    • @reubenkurian1155
      @reubenkurian1155 4 роки тому

      @@ProgrammerWorld thank you ...it worked ...any way we can add multiple images in one pdf ?

  • @iqratariq9745
    @iqratariq9745 2 роки тому

    Sir, how to make a pdf file of multiple jpg images at a time?

    • @ProgrammerWorld
      @ProgrammerWorld  2 роки тому

      Try to add multiple pages as below and keep printing the images into the pages.
      // 1st Page
      PdfDocument.PageInfo myPageInfo1 = new PdfDocument.PageInfo.Builder(960,1280,1).create();
      PdfDocument.Page page1 = pdfDocument.startPage(myPageInfo1);
      page1.getCanvas().drawBitmap(bitmap1,0,0, null);
      pdfDocument.finishPage(page1);
      // 2nd Page
      PdfDocument.PageInfo myPageInfo2 = new PdfDocument.PageInfo.Builder(960,1280,2).create();
      PdfDocument.Page page2 = pdfDocument.startPage(myPageInfo2);
      page2.getCanvas().drawBitmap(bitmap2,0,0, null);
      pdfDocument.finishPage(page2);
      // ... so on for further pages.
      It should work.
      Cheers
      Programmer World
      programmerworld.co/android/how-to-convert-jpg-image-file-into-pdf-file-in-your-android-app-complete-source-code/
      -

    • @iqratariq9745
      @iqratariq9745 2 роки тому +1

      @@ProgrammerWorld Thanks it worked.

  • @pakgeochannel8419
    @pakgeochannel8419 3 роки тому

    The image you set via Drawable. In PDF file
    That's how to set the image via imageview. In PDF file
    What is java coding
    وہ تصویر جس کو آپ نے ڈرا ایبل کے ذریعے سیٹ کیا۔ پی ڈی ایف فائل میں
    وہ ہی تصویرکو تصویرویو کے ذریعے سیٹ کرنے کا طریقہ کیا ہے۔ پی ڈی ایف فائل میں

    • @ProgrammerWorld
      @ProgrammerWorld  3 роки тому

      I think the java code is available on our website: programmerworld.co
      Please let us know in case of issue in accessing it.
      Cheers
      Programmer World
      programmerworld.co
      -

  • @medislambengammem4268
    @medislambengammem4268 3 роки тому

    KOTLIN code please !

    • @ProgrammerWorld
      @ProgrammerWorld  3 роки тому +1

      I think we can easily covert the Java code into Kotlin using the steps shown in my below link:
      programmerworld.co/android/how-to-convert-the-java-code-to-kotlin-for-your-app-in-android-studio/
      Cheers
      Programmer World
      programmerworld.co/
      -

    • @medislambengammem4268
      @medislambengammem4268 3 роки тому

      @@ProgrammerWorld thanks

  • @akshayg7103
    @akshayg7103 5 років тому

    Not working getExternalStorage ()

    • @ProgrammerWorld
      @ProgrammerWorld  5 років тому

      I think you should try using: getExternalStoragePublicDirectory()

    • @akshayg7103
      @akshayg7103 5 років тому

      I used that not working just on code black line comes on it . I think that word is deprecated

    • @ProgrammerWorld
      @ProgrammerWorld  5 років тому

      @@akshayg7103
      On checking my code shown in the video, I have used minSdkVersion as 28 and it worked. So, can you please try the same.

    • @akshayg7103
      @akshayg7103 5 років тому

      Can u me the code to convert pdf to word

    • @akshayg7103
      @akshayg7103 5 років тому

      @@ProgrammerWorld u just 1 folder and images if i want to take image from any folder from my phone