android下载后打开pdf

博客展示了Android代码,包含获取PDF文件意图的方法,以及点击按钮下载PDF文件的逻辑。通过开启新线程下载文件,避免干扰主UI线程,下载成功后调用方法打开PDF文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 
 /**
     * 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;
	}
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);
				}
			}
		});


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值