我已经创建了一个应用程序,我通过URL成功下载了来自互联网的pdf文件。并通过创建文件夹app_Pdf将它们放到应用程序的内部存储中。但现在我想用第三方应用程序打开该文件,如adobe pdf viewer ...等。我已经尝试了很多方法,对于这个问题我已经做了很多事情。我还阅读了关于制作第一个内容提供者并通过此任务提供pdf文件:How to open a PDF stored in Internal Memory但我收到错误:无效路径....
请朋友帮我解决这个问题。如果给我一些工作代码会很好 非常感谢你提前.....
这是我在内存中下载和存储文件的方式:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
downloadFile("http://www.testdemo.net/testdemo/test/test/test.pdf");
}
public boolean downloadFile(String path)
{
try
{
URL url = new URL(path);
URLConnection ucon = url.openConnection();
ucon.setReadTimeout(5000);
ucon.setConnectTimeout(10000);
InputStream is = ucon.getInputStream();
BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5);
File file = new File(getDir("Pdf", Context.MODE_WORLD_READABLE) + "/yourfile.pdf");
if (file.exists())
{
file.delete();
}
file.createNewFile();
FileOutputStream outStream = new FileOutputStream(file);
byte[] buff = new byte[5 * 1024];
int len;
while ((len = inStream.read(buff)) != -1)
{
outStream.write(buff, 0, len);
}
outStream.flush();
outStream.close();
inStream.close();
}
catch (Exception e)
{
e.printStackTrace();
return false;
}
return true;
}
}
这就是我试图从内部存储中查看pdf的方式:
File file = new File(getDir("Pdf", Context.MODE_PRIVATE) + "/yourfile.pdf");
Uri internal = Uri.fromFile(file);
viewPdf(internal);
private void viewPdf(Uri file) {
Intent intent;
intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(file, "application/pdf");
try {
startActivity(intent);
} catch (ActivityNotFoundException e) {
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://details?id=com.adobe.reader"));
startActivity(marketIntent);
}
});
builder.setNegativeButton("No, Thanks", null);
builder.create().show();
Log.v("","Exception : "+e);
}
请帮助我....