【发布时间】:2012-02-29 09:20:41
【问题描述】:
现在我正在做一个 Android 应用程序。我必须从 sdcard 读取文件。我在模拟器中安装了 adobe reader。我使用以下代码从 sdcard 运行文件。
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse("/sdcard/samp.pdf"), "application/pdf");
try{
startActivity(intent);
}
catch (ActivityNotFoundException e) {
System.out.println("5");
// No application to view, ask to download one
System.out.println("hai");
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() {
public void onClick(DialogInterface dialog, int which) {
Intent marketIntent = new Intent(Intent.ACTION_VIEW);
marketIntent
.setData(Uri
.parse("market://details?id=com.adobe.reader"));
startActivity(marketIntent);
}
});
builder.setNegativeButton("No, Thanks", null);
builder.create().show();
}
我在 sdcard 中插入了一个名为 sample.pdf 的 pdf。但是当我运行我的应用程序时,我得到“无法打开文件”。但我可以从模拟器手动打开pdf。请帮我找出解决方案。
嗨,朋友们,我得到了解决方案。我删除了
intent.setDataAndType(Uri.parse("/sdcard/samp.pdf"), "application/pdf");
并添加
File file = new File("/sdcard/sample.pdf");
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/pdf");
现在它的工作。但是现在我把sample.pdf文件夹放到我的assets文件夹里,写下如下代码。
File file = new File("android.resource://com.pdftest/assets/sample");
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/pdf");
我收到文件路径无效消息...
【问题讨论】:
标签: android