How to open pdf on Android?

Asked

Viewed 1,930 times

1

In my application there is a button where when clicked, will open a arquivopdf, but could not get this file from the internet, would have to come along with the application, I am using the following code :

Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("aqui tentei muitas coisas, mas nenhuma deu certo"), "aplication/pdf");
startActivity(intent);

On the part of Uri I don’t know what to put, since I tried many ways, and I put my file in the folder assets and in the raw also...

2 answers

1

If you want to open your folder assets try something like this:

private void readFromAssets(String filename) {
    AssetManager assetManager = getAssets();

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

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

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

    Intent intent = Intent.createChooser(target, "Abrir arquivo");

    try {
        startActivity(intent);
    } catch (ActivityNotFoundException e) {
        //Caso o usuário não tenha um visualizador de PDF, instrua-o aqui a baixar
    } 
}

If you want to open from the device root, try:

private void readFromExternalStorage(String filename){
    File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ filename);
    Intent target = new Intent(Intent.ACTION_VIEW);
    target.setDataAndType(Uri.fromFile(file), "application/pdf");

    Intent intent = Intent.createChooser(target, "Abrir arquivo");

    try {
        startActivity(intent);
    } catch (ActivityNotFoundException e) {
        //Caso o usuário não tenha um visualizador de PDF, instrua-o aqui a baixar
    }
}

1

Sorry, I’m beginner on Android and did not understand how I should use this code, I put it on a button, but gave some error in the parentheses in line " botao.setOnClickListener(new View.Onclicklistener(){ "

botao.setOnClickListener(new View.OnClickListener(){
         @Override
         public void onClick(View v){
            /*Intent beintent  = new Intent(Intent.ACTION_VIEW);
            Uri uri = Uri.parse("android.resource://" + getPackageName() + "/" + R.raw.pdf);
            beintent.setDataAndType(uri, "application/pdf");
            startActivity(beintent);
         }*/
            public void readFromAssets(String filename) {
                AssetManager assetManager = getAssets();

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

                    copyFile(in, out);
                    in.close();
                    in = null;
                    out.flush();
                    out.close();
                    out = null;
                } catch (Exception e) {
                    Toast.makeText(Codificacao.this,"Erro", Toast.LENGTH_LONG).show();

                }

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

                Intent intent = Intent.createChooser(target, "Abrir arquivo");

                try {
                    startActivity(intent);
                } catch (ActivityNotFoundException e) {
                    //Caso o usuário não tenha um visualizador de PDF, instrua-o aqui a baixar
                } 
            }

         }



        });
}



private void copyFile(InputStream in, OutputStream out) {
        // TODO Auto-generated method stub

    }

In parentheses is another method I tried but failed...

Browser other questions tagged

You are not signed in. Login or sign up in order to post.