android open pdf,打开带有openwith选项android的pdf

试试这个

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ filename);

Intent target = new Intent(Intent.ACTION_VIEW);

target.setDataAndType(Uri.fromFile(file),"application/pdf");

target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

Intent intent = Intent.createChooser(target, "Open File");

try {

startActivity(intent);

} catch (ActivityNotFoundException e) {

// Instruct the user to install a PDF reader here, or something

}

编辑1

OutputStream out = new FileOutputStream("out.pdf");

out.write(bArray);

out.close();

创建pdf后,

File file = new File("filepath");

Intent target = new Intent(Intent.ACTION_VIEW);

target.setDataAndType(Uri.fromFile(file),"application/pdf");

target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

Intent intent = Intent.createChooser(target, "Open File");

try {

startActivity(intent);

} catch (ActivityNotFoundException e) {

// Instruct the user to install a PDF reader here, or something

}

编辑2

File myFile = new File("out.pdf");

OutputStream out = new FileOutputStream(myFile);

out.write(bytArray);

out.close();

Intent target = new Intent(Intent.ACTION_VIEW);

target.setDataAndType(Uri.fromFile(myFile),"application/pdf");

target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

Intent intent = Intent.createChooser(target, "Open File");

try {

startActivity(intent);

} catch (ActivityNotFoundException e) {

// Instruct the user to install a PDF reader here, or something

}

这可能对你有所帮助

编辑3

以下代码由我自己测试,它可以按照您的需要运行

Create the pdf file:

File resolveMeSDCard = new File("/sdcard/download/media/output.pdf");

public void createPDF()

{

byte[] byt = new byte[]{1,2,3,4,5};

File mediaDir = new File("/sdcard/download/media");

if (!mediaDir.exists()){

mediaDir.mkdir();

}

FileOutputStream fos;

try {

//File resolveMeSDCard = new File("/sdcard/download/media/output.pdf");

resolveMeSDCard.createNewFile();

fos = new FileOutputStream(resolveMeSDCard);

fos.write(byt);

fos.close();

System.out.println("Your file has been written");

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

System.out.println("Your file has not been written");

}

}

Open the pdf file:

public void openPDF()

{

Intent target = new Intent(Intent.ACTION_VIEW);

target.setDataAndType(Uri.fromFile(resolveMeSDCard),"application/pdf");

target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);

Intent intent = Intent.createChooser(target, "Open File");

try {

startActivity(intent);

} catch (ActivityNotFoundException e) {

// Instruct the user to install a PDF reader here, or something

}

}

manifest.xml

添加以下权限

注意 :

1.根据需要更改代码顺序.

2.call createPDF()然后打开OpenPDF().

这是工作代码.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值