Mayla Campos
Mayla Campos

Reputation: 11

How to open a pdf in android by pressing a button?

I'm creating an app to open a PDF application from a button, but what I did is not going well. In my layout there is only one button , which seeks want and open a PDF file. Not giving error in Eclipse , just in tablet appears " readerPDF stopped ." I do not know what to do. somebody help me.

Sorry for my english.

public class ReaderActivity extends Activity {

    private Button btnOpen;

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

        btnOpen.setOnClickListener(new OnClickListener() {
            public void onClick(View arg0) {

                String filename = null;
                File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ filename);
                Intent target = new Intent(Intent.ACTION_VIEW);
                target.setDataAndType(Uri.fromFile(file),"application/pdf");
                target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

                Intent intent = Intent.createChooser(target, "Open File");
                try {
                    startActivity(intent);
                } catch (ActivityNotFoundException e) {

                } 
            }
        });
    }

}

Upvotes: 1

Views: 1751

Answers (1)

CommonsWare
CommonsWare

Reputation: 1007296

filename is null, so the path you are passing to the third-party app does not point to a PDF file.

Upvotes: 3

Related Questions