How to Convert PNG/JPG Images to PDF Document App in Android Studio Using Java Full Project For Beginners

 

Welcome folks today in this blog post we will be converting images to pdf document in android studio using java.All the full source code of the application is shown below.

 

 

 

Get Started

 

 

 

java 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();

}
}

 

 

 

xml code

 

 

 

<?xml version=”1.0″ encoding=”utf-8″?>
<RelativeLayout xmlns:android=”http://schemas.android.com/apk/res/android”
xmlns:app=”http://schemas.android.com/apk/res-auto”
xmlns:tools=”http://schemas.android.com/tools”
android:layout_width=”match_parent”
android:layout_height=”match_parent”
tools:context=”.MainActivity”>

<Button
android:id=”@+id/button”
android:layout_width=”wrap_content”
android:layout_height=”wrap_content”
android:layout_marginStart=”100dp”
android:layout_marginTop=”250dp”
android:onClick=”convertButton”
android:text=”@string/convert_jpg_to_pdf” />
</RelativeLayout>

 

 

Now we need to add the external permission storage inside the manifest file

READ  How to Dial or Call a Phone Number Programmatically in Android App Using Android Studio and Java Full Project For Beginners

 

 

<?xml version=”1.0″ encoding=”utf-8″?>
<manifest xmlns:android=”http://schemas.android.com/apk/res/android”
package=”com.example.myjpgtopdf”>

<uses-permission android:name=”android.permission.WRITE_EXTERNAL_STORAGE”/>

<application
android:allowBackup=”true”
android:icon=”@mipmap/ic_launcher”
android:label=”@string/app_name”
android:roundIcon=”@mipmap/ic_launcher_round”
android:supportsRtl=”true”
android:theme=”@style/AppTheme”>
<activity android:name=”.MainActivity”>
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>

</manifest>

Leave a Reply