Android Open pdf file programmatically Example

Android Open pdf file programmatically Example

In this Post We Will Explain About is Android Open pdf file programmatically Example With Example and Output for how to open pdf file in android programmatically Demo.Welcome on Live24u.com – Examples, The best For Learn web development Tutorials,Demo with Example! Hi Dear Friends here u can know to how to open pdf file in android programmatically Example

In this post we will show you Best way to implement how to open pdf file in android programmatically, hear for how to open pdf file in android programmatically with Download .we will give you demo,Source Code and examples for implement Step By Step Good Luck!.




In this Article we will Display step by step that how to open PDF file or read YOUR pdf file programmatically in android Application. If any client or user open android mobile app then read data pdf file will be open.so First of all put your particuler given pdf file Path in app\src\main\assets Base path .Therefor You can any here global path customise according to your any requirements.

Also Read :   WhatsAPP send and receive Message API using PHP Step By Step

We have shown here with Two types of the option :

First is given your pdf file will read or open after mobile app open .

Second is the when client trigger or click on custom any btn button then given read pdf file will be basiclly open.

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.cacompadda.openpdf"> //package name openpdf

	//mobile app settings
    <application
        android:allowBackup="true"
        android:icon="@mipmap/icon"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
		//start Activity
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name"
            android:theme="@style/AppTheme.NoActionBar">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

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

</manifest>

MainActivity.java

Source code : how to open pdf file in android programmatically

// android name space and include class
package com.cacompadda.openpdf;
import android.content.Context;//For Context
import android.content.Intent;//For Intent
import android.content.res.AssetManager;
import android.net.Uri;  //uri basic
import android.os.Bundle;//For Bundle
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.util.Log; //print logs
import android.view.View;//For android Display
import android.support.design.widget.NavigationView;
import android.support.v4.view.GravityCompat; //For GravityCompat
import android.support.v4.widget.DrawerLayout; //For DrawerLayout
import android.support.v7.app.ActionBarDrawerToggle;//For ActionBarDrawerToggle
import android.support.v7.app.AppCompatActivity;//For AppCompatActivity
import android.support.v7.widget.Toolbar;//For Toolbar
import android.view.Menu;
import android.view.MenuItem;

//java Input and output files
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

//start methods MainActivity
public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

		//Methods Override
    @Override
    protected void onCreate(Bundle savedInstanceState) {//start android lifecycle
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

		//for design FloatingActionButton
        FloatingActionButton livefab = (FloatingActionButton) findViewById(R.id.livefab);
        livefab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

		//add and use DrawerLayout 
        DrawerLayout livedrw = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle livetgl = new ActionBarDrawerToggle(
                this, livedrw, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        livedrw.setDrawerListener(livetgl);
        livetgl.syncState();

		// android NavigationView here
        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);
        CopyReadPDFFromAssets();
    }

    @Override
	//Methods Override
    public void onBackPressed() {
        DrawerLayout livedrw = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (livedrw.isDrawerOpen(GravityCompat.START)) {
            livedrw.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
	//Methods Override
    public boolean onCreateOptionsMenu(Menu menu) {

        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem packproduct) {

        int id = packproduct.getItemId();

        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(packproduct);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
	//Methods Override
    public boolean onNavigationItemSelected(MenuItem packproduct) {
        int id = packproduct.getItemId();

        if (id == R.id.nav_gallery) {
            Intent i =new Intent(this,MainActivity.class);
            startActivity(i);
        }

        DrawerLayout livedrw = (DrawerLayout) findViewById(R.id.drawer_layout);
        livedrw.closeDrawer(GravityCompat.START);
        return true;
    }
    private void CopyReadPDFFromAssets()
    {
        AssetManager assetManager = getAssets();

        InputStream in = null;
        OutputStream out = null;
        File file = new File(getFilesDir(), "live24u.pdf");
        try
        {
            in = assetManager.open("live24u.pdf");
            out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);

            livePdfFileCp(in, out);
            in.close();
            in = null;
            out.flush();
            out.close();
            out = null;
        } catch (Exception e)
        {
            Log.e("exception", e.getMessage());
        }

        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setDataAndType(
                Uri.parse("file://" + getFilesDir() + "/live24u.pdf"),
                "application/pdf");

        startActivity(intent);
    }

    private void livePdfFileCp(InputStream in, OutputStream out) throws IOException
    {
        byte[] buffer = new byte[1024];
        int read;
        while ((read = in.read(buffer)) != -1)
        {
            out.write(buffer, 0, read);
        }
    }
    public void pdfaskdata(View v){
		//start switch conditions
        switch (v.getId()){
            case R.id.openpdf:       //here openpdf or read is id of button
                CopyReadPDFFromAssets();
                break;
           
            default:
                break;
        }
    }
}

content_main.xml

only button source code for how to open pdf file in android programmatically

Also Read :   IPL 2017 Points Table IPL 10 Team Rankings IPL Results

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp">
        <Button
            android:id="@+id/openpdf"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onClick="pdfaskdata"
            android:text="Read PDF"
            android:background="#3d3d3d" />
    </LinearLayout>

MainAcitivity Screen :

Here show how to open pdf file in android programmatically

Open or read pdf file programmatically step by step in android output

In this Best way you can read or open your pdf file in your mobile app step by step programmatically. If you have any Query or question then comment below.

You are Most welcome in my youtube Channel Please subscribe my channel. and give me FeedBack.
More Details……
Angularjs Example





Example

Also Read :   Top 10 Best SmartPhones overall in World

I hope you have Got What is How to open pdf file programmatically in android And how it works.I would Like to have FeedBack From My Blog(live24u.com) readers.Your Valuable FeedBack,Any Question,or any Comments about This Article(live24u.com) Are Most Always Welcome.