问题
I am trying to open an epub file from my android app using Moon Reader. Code given below. I am getting an error in the log as follows: 11-26 16:57:01.237: E/Web Console(12254): Uncaught Error: Error calling method on NPObject.:5914
Please can you advise what can be the issue?
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();
}
}
回答1:
Try this
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
}
}
And as parameter send path to the file
回答2:
The same code worked for me without any change. Error was in some different part of code.
来源:https://stackoverflow.com/questions/20216203/open-an-epub-file-from-android-app