Open top menu


Read PDF file via assets folder in Android Studio


In this tutorial explain how to read pdf file assets folder.
I have used PDF Read Library .

Step 1. Create Android Project.

Step 2. Add Permission in Manifest file.

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

Step 3.Add dependencies .
compile 'com.github.barteksc:android-pdf-viewer:2.7.0-beta.1'
Step 4. Create activity_main.xml.
 
xml version="1.0" encoding="utf-8"?> <LinearLayout 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"     android:orientation="vertical">     <Button         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:text="Android Begginer Point"         android:textAllCaps="true"         android:textStyle="bold"         android:textColor="#FF439934"         />     <com.github.barteksc.pdfviewer.PDFView         android:id="@+id/pdfView"         android:layout_width="match_parent"         android:layout_height="match_parent" /> </LinearLayout>
Step 5. Create MainActivity.java.
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.pdf.PdfRenderer;
import android.net.Uri;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
import android.widget.Toast;

import com.github.barteksc.pdfviewer.PDFView;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class MainActivity extends AppCompatActivity {

    private static final String FILENAME = "sit.pdf";
    PDFView ReadTxt;
   
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ReadTxt=(PDFView)findViewById(R.id.pdfView);
        File file = new File(this.getCacheDir(), FILENAME);
        if (!file.exists()) {
            try {
                InputStream asset = this.getAssets().open(FILENAME);
                ReadTxt.fromStream(asset)
                        .pages(0, 2, 1, 3, 3, 3)
                        .enableSwipe(true)
                        .swipeHorizontal(false)
                        .enableDoubletap(true)
                        .defaultPage(0)
                        .enableAnnotationRendering(false)
                        .password(null)
                        .scrollHandle(null)
                        .enableAntialiasing(true)
                        .spacing(0)
                        .load();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }else {
           Toast.makeText(getApplicationContext(),
"File not found",Toast.LENGTH_LONG).show();
        }
    }

    @Override    protected void onStart() {
        super.onStart();
    }

}



try this.........




Tagged

1 comment: