gpt4 book ai didi

android - 使用 openwith 选项 android 打开 pdf

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:20:43 26 4
gpt4 key购买 nike

我有一个用我自己的应用程序打开的 pdf。现在,当用户点击一个按钮时,我会显示 application/pdf 的 openwith 选项。现在用户选择他的选择(例如 adobe reader)并且打开的 pdf 文件必须显示在用户选择中(在这种情况下为 adobereader)。对于打开的 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

添加以下权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

注意:
1.根据需要更改代码顺序。

2.调用 createPDF(),然后调用 OpenPDF()。

这是工作代码。

关于android - 使用 openwith 选项 android 打开 pdf,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38178349/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com