您的位置:首页 > 移动开发 > Android开发

Android:下载后打开PDF格式文件

2014-06-11 15:43 555 查看
File file = new File("/sdcard/example.pdf");

if (file.exists()) {

Uri path = Uri.fromFile(file);

Intent intent = new Intent(Intent.ACTION_VIEW);

intent.setDataAndType(path, "application/pdf");

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

try {

startActivity(intent);

}

catch (ActivityNotFoundException e) {

Toast.makeText(OpenPdf.this,

"No Application Available to View PDF",

Toast.LENGTH_SHORT).show();

}

}

上面的可以正常打开的

Uri path = Uri.fromFile(file);

Intent intent = new Intent(Intent.ACTION_VIEW);

intent.setDataAndType(path, "application/pdf");

intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

try {

startActivity(intent);

} catch (ActivityNotFoundException e) {

Toast.makeText(this,

"No Application Available to View PDF",

Toast.LENGTH_SHORT).show();

}
http://blog.csdn.net/xiaojunhu/article/details/8814710 http://blog.csdn.net/ouyangtianhan/article/details/6975899
首先是下载,不再赘述。

然后定义打开pdf的Intent

[java] view
plaincopy

/**

* Get PDF file Intent

*/

public Intent getPdfFileIntent(String path){

Intent i = new Intent(Intent.ACTION_VIEW);

i.addCategory(Intent.CATEGORY_DEFAULT);

i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK );

Uri uri = Uri.fromFile(new File(path));

i.setDataAndType(uri, "application/pdf");

return i;

}

打开

[java] view
plaincopy

btnDownload.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {

//You need to open another thread to download file ,

//so that it cannot disturb the main UI thread.

final HttpDownloader loader = new HttpDownloader(getApplicationContext());

int result = loader.download("http://xxx.pdf", "Tian/", "t0.pdf");

Log.d("PDFActivity.java","Download result: "+result);

if(result == 0 || result == 1){ // Success,open it

Intent intent = getPdfFileIntent("/mnt/sdcard/Tian/t0.pdf");

startActivity(intent);

}

}

});
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: