Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
1. create assets folder in src under your app name.
2. In this assets folder keep your pdf files e.g. file.pdf.
3 now come your activity i.e MainActivity.java
4. setListener on any UI component what you want i.e (Button,ImageView,ImageButton);
5. In this listener call one user defined method i.e. openPDFFiles("file.pdf");
the openPDFFiles() method have below code:—-
private void openPDFFiles(String fileName) //fileName is the pdf file name which is keep in assets folder. ex file.pdf
{
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), "");
try {
in = assetManager.open(fileName);
out = openFileOutput(file.getName(), MODE_PRIVATE);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e) {
Log.e(TAG, e.getMessage());
}
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("file://" + getFilesDir() + "/"+fileName), "application/pdf");
startActivity(intent);
}catch (RuntimeException ex){
Toast.makeText(MainActivity.this, "There's no PDF Reader found in your device", Toast.LENGTH_SHORT).show();
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException
{
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1)
{
out.write(buffer, 0, read);
}
}
@Omnyyah

This comment has been minimized.

Copy link

Omnyyah commented Feb 23, 2016

Thank you ,this is really helpful .

@mechanicpokemon

This comment has been minimized.

Copy link

mechanicpokemon commented Jan 2, 2017

i dons understand, would you share the step by step proccedure?

@lolxd123456

This comment has been minimized.

Copy link

lolxd123456 commented Jul 5, 2018

45

@rmbarreto

This comment has been minimized.

Copy link

rmbarreto commented Jul 19, 2018

Notice that the intent.setDataAndType(Uri.parse("file://" + getFilesDir() + "/"+fileName), "application/pdf"); is not working anymore for Version <24

http://onetouchcode.com/2017/05/24/android-android-os-fileuriexposedexception-reason/

@itechsyst

This comment has been minimized.

Copy link

itechsyst commented Aug 21, 2018

I have used this piece of code ,i am new to android,I cannot open the .pdf files of my html/php from assets folder what necessary changes should make in code so that it works??

package co.itechsyst.srinot;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.KeyEvent;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.content.Intent;
import android.net.Uri;
import android.content.ActivityNotFoundException;

public class Main2Activity extends AppCompatActivity {

WebView myPersonalsite;
String URL;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);

    URL="file:///android_asset/srinot/main.php";
    myPersonalsite=(WebView) findViewById(R.id.myPersonalsite);

    myPersonalsite.getSettings().setJavaScriptEnabled(true);
    myPersonalsite.loadUrl(URL);
    myPersonalsite.setWebViewClient(new WebViewClient() {

                                        @Override
                                        public boolean shouldOverrideUrlLoading(WebView view, String url) {
                                            if (url.endsWith(".pdf")) {
                                                
                                                Intent target = new Intent(Intent.ACTION_VIEW);
                                                target.setDataAndType(Uri.parse(url), "application/pdf");
                                                target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

                                                Intent intent = Intent.createChooser(target, "Open File");
                                                try {
                                                    view.getContext().startActivity(intent);
                                                } catch (ActivityNotFoundException e) {
                                                    //user does not have a pdf viewer installed
                                                }
                                            } else {
                                                myPersonalsite.loadUrl(url);
                                            }
                                            return true;
                                        }
                                    }
    );
}




@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
    if (event.getAction() == KeyEvent.ACTION_DOWN) {
        switch (keyCode) {
            case KeyEvent.KEYCODE_BACK:
                if (myPersonalsite.canGoBack()) {
                    myPersonalsite.goBack();
                } else {
                    finish();
                }
                return true;
        }

    }
    return super.onKeyDown(keyCode, event);
}

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.