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();
}
}
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?
sorry but my bitmap is null , and where did the getCanvas came from? the galley does not open
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
-
How to convert multiple image in PDF file pleases give this solution
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
-
How to add an image with hyperlink in pdf
Can you choose any picture without writing its name
Can I add a number of texts with the image?
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
-
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?
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
-
sir please make video on JPG to video or mp4
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
-
Thanks man means alot
wrote the exact steps but the app is crashing as soon as i press the button
it says "unfortunately has stopped working" ...any solution?
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
-
@@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?
all permissions are also given ..still its crashing
@@ProgrammerWorld thank you ...it worked ...any way we can add multiple images in one pdf ?
Sir, how to make a pdf file of multiple jpg images at a time?
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/
-
@@ProgrammerWorld Thanks it worked.
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
وہ تصویر جس کو آپ نے ڈرا ایبل کے ذریعے سیٹ کیا۔ پی ڈی ایف فائل میں
وہ ہی تصویرکو تصویرویو کے ذریعے سیٹ کرنے کا طریقہ کیا ہے۔ پی ڈی ایف فائل میں
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
-
KOTLIN code please !
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/
-
@@ProgrammerWorld thanks
Not working getExternalStorage ()
I think you should try using: getExternalStoragePublicDirectory()
I used that not working just on code black line comes on it . I think that word is deprecated
@@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.
Can u me the code to convert pdf to word
@@ProgrammerWorld u just 1 folder and images if i want to take image from any folder from my phone