从Android应用程序打开一个epub文件

时间:2013-11-26 11:45:46

标签: android epub

我正在尝试使用Moon Reader从我的Android应用程序打开一个epub文件。代码如下。我在日志中收到错误如下: 11-26 16:57:01.237:E / Web控制台(12254):未捕获错误:在NPObject上调用方法时出错。:5914

请问您可以提出什么问题?

public void openEpub(String url)
{
    Intent intent = null;
    Uri uri = null;
    try {
        intent = new Intent(Intent.ACTION_VIEW);
        uri = Uri.parse(url);
        intent.setDataAndType(uri, "application/epub+zip");
    } catch (Exception e) {
        e.printStackTrace();
    }


    try {
        //startActivity(intent);
        PackageManager pm = getPackageManager();
        try
        {
            String packageName = "com.flyersoft.moonreader";
            Intent launchIntent = pm.getLaunchIntentForPackage(packageName);
            launchIntent.setAction(Intent.ACTION_VIEW);
            launchIntent.setData(uri);
            startActivity(launchIntent);
        }
        catch (Exception e1)
        {
            e1.printStackTrace();
        }
        catch (Error e2) {
            e2.printStackTrace();
        }
    } catch (ActivityNotFoundException e) {
        // No application to view, ask to download one
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setTitle("No Application Found");
        builder.setMessage("Download one from Android Market?");
        builder.setPositiveButton("Yes, Please",
                new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent marketIntent = new Intent(Intent.ACTION_VIEW);
                        marketIntent.setData(Uri.parse("market://search?q=epub&c=apps"));
                        startActivity(marketIntent);
                    }
                });
        builder.setNegativeButton("No, Thanks", null);
        builder.create().show();
    }
    catch(Error e3) {
        e3.printStackTrace();
    }
}

2 个答案:

答案 0 :(得分:0)

试试这个

private void openReader(String data)
{
    Intent i;       
    PackageManager manager = getPackageManager();
    try {
        i = manager.getLaunchIntentForPackage("com.flyersoft.moonreader");
        if (i == null)
            throw new PackageManager.NameNotFoundException();

        i.setAction(Intent.ACTION_VIEW);
        i.setData(Uri.parse(data));
        startActivity(i);
    } catch (PackageManager.NameNotFoundException e) {
                   //Do something
    }
}

作为参数发送到文件的路径

答案 1 :(得分:-1)

相同的代码对我没有任何改变。错误出现在代码的某些不同部分。