Java 在Android中打开pdf时出现问题:文件路径无效
我需要从我的android应用程序打开一个pdf文件。我已将pdf保存在应用程序包文件夹/data/data/com.app.example/files中。我已经在android模拟器中安装了AdobeReader应用程序。问题是,当我尝试用adobe reader打开pdf文件时,emulator会向我显示下一条消息:无效的文件路径 我不知道为什么会发生这种情况,但我被困在这一点上了。文件保存正确,因为我可以在计算机中打开它 代码如下:Java 在Android中打开pdf时出现问题:文件路径无效,java,android,pdf,Java,Android,Pdf,我需要从我的android应用程序打开一个pdf文件。我已将pdf保存在应用程序包文件夹/data/data/com.app.example/files中。我已经在android模拟器中安装了AdobeReader应用程序。问题是,当我尝试用adobe reader打开pdf文件时,emulator会向我显示下一条消息:无效的文件路径 我不知道为什么会发生这种情况,但我被困在这一点上了。文件保存正确,因为我可以在计算机中打开它 代码如下: HttpClient client = new Defa
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet(url);
try {
HttpResponse response = client.execute(request);
request(response, file_name);
File file = new File("data/data/com.app.example/files/"+file_name);
PackageManager packageManager = getPackageManager();
Intent testIntent = new Intent(Intent.ACTION_VIEW);
testIntent.setType("application/pdf");
List list = packageManager.queryIntentActivities(testIntent, PackageManager.MATCH_DEFAULT_ONLY);
if(file.exists())
{
if (list.size() > 0 && file.isFile()) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
}else
{
System.out.println("NO APPs TO OPEN PDFs.");
}
}
else
{
System.out.println("FILE DOES NOT EXIST.");
}
}catch(Exception ex){
System.out.println("Failed!");
ex.printStackTrace();
}
public String request(HttpResponse response, String file){
String result = "";
try{
InputStream in = response.getEntity().getContent();
FileOutputStream f = openFileOutput(file, MODE_WORLD_WRITEABLE);
byte[] buffer = new byte[in.toString().length()];
int len1 = 0;
int counter = 0;
while ((len1 = in.read(buffer)) > 0) {
f.write(buffer, 0, len1);
counter++;
}
f.close();
}
catch(Exception ex){
result = "Error";
}
return result;
}
提前谢谢
尊敬的Javi。请不要使用直接路径访问应用程序私有目录中的文件。改用
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
请参阅:不要使用直接路径访问应用程序专用的文件。改用
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
请参阅:这是因为PDF文件驻留在您自己的软件包中,Adobe reader试图从您的软件包中访问PDF文件,这是Android应用程序开发所不允许的
您可以尝试将文件保存在SD卡上,然后打开它这是因为PDF文件位于您自己的软件包中,而Adobe reader试图从您的软件包中访问PDF文件,这是Android应用程序开发所不允许的
您可以尝试将文件保存在SD卡上,然后打开它谢谢Drax。这让我继续开发xD。谢谢Drax。这让我继续开发xD。