简体   繁体   中英

I can not open a PDF in my android application

I made an android application. I created a extensible list, when I click on a child I want to be open a PDF. The PDF is not open, is displayed a message that is content from no_application_found ("The PDF can not be open"). The code for open PDF is:

    public boolean onChildClick (
        ExpandableListView parent, 
        View v, 
        int groupPosition,
        int childPosition,
        long id) {
    Log.d( LOG_TAG, "onChildClick: " + childPosition );

    File file = new File("http://www.ratt.ro/grafice/e2-a.pdf");
    Uri path = Uri.fromFile(file);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    intent.setDataAndType(path, getString(R.string.application_type));
    try 
    {
        startActivity(intent);
    } 
    catch (ActivityNotFoundException e) 
    {
        Toast.makeText(ElistCBox.this, 
            getString(R.string.no_application_found), 
            Toast.LENGTH_SHORT).show();
    }

    return false;
}

这是您在网络视图中打开PDF的方式:

webview.loadUrl("http://docs.google.com/gview?embedded=true&url=http://www.ratt.ro/grafice/e2-a.pdf");

Here is my code for opening PDF file in reader, if user do not have any pdf reader it opens link to Google play for downloading one:

void openpdf (String filename)

{
    String loc = "/sdcard/";
    File file = new File(loc+filename);
    Uri path = Uri.fromFile(file);
    Intent intent = new Intent(Intent.ACTION_VIEW);
    intent.setDataAndType(path, "application/pdf");
    intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

    try {
        startActivity(intent);
    } catch (Exception e) {

        e.printStackTrace();
        Toast.makeText(getApplicationContext(), "No pdf reader installed", Toast.LENGTH_SHORT).show();
        Intent market = new Intent(Intent.ACTION_VIEW);
        market.setData(Uri.parse("market://details?id=com.adobe.reader"));
        startActivity(market);
    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM