How to open pdf on Android?

1

In my application there is a button where when clicked, it will open a arquivo pdf, 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);

In the Uri part I do not know what to put, since I have already tried many ways, and I put my file in the folder assets and raw too ...

    
asked by anonymous 05.06.2015 / 21:15

2 answers

1

If you want to open your assets folder 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 root of the device, 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
    }
}
    
05.06.2015 / 21:35
1

Sorry, I'm a beginner on Android and I did not understand how I should use this code, I put it in a button, but it gave some errors in the parentheses in the 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 without success ...

    
06.06.2015 / 00:57