【问题标题】:open pdf with openwith option android使用 openwith 选项打开 pdf android
【发布时间】:2016-07-04 06:44:51
【问题描述】:

我有一个用我自己的应用程序打开的 pdf。现在,当用户单击一个按钮时,我将显示 openwith 应用程序/pdf 选项。现在用户选择他的选择(例如adobe reader),打开的pdf文件必须显示在用户选择(本例中为adobereader)中。对于打开的 PDF,我有 bytearray 和输入流。

【问题讨论】:

  • 你的问题是什么?
  • 你好,默认会显示。但是到目前为止你尝试了什么?
  • openwith 窗口将默认显示,但是当我在我的应用程序中单击一个按钮时,我会显示 openwith 意图并且现在用户选择 adobe reader,现在我想在 adobereader 中显示相同的 pdf 文档 pdf 字节。实际上我是 android 新手,无法启动或识别此处触发的事件....

标签: android pdf


【解决方案1】:

试试这个

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

以下代码是我自己测试的,它可以按你的意愿工作

创建 pdf 文件:

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");  
    }

  }

打开pdf文件:

  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" />

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

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

这是工作代码。

【讨论】:

  • 感谢 Sathish 的回复。但我的文件在物理上不可用,我在 bytearray 和 inputstream 中也有文件...
  • 然后首先从您的 byteArray 创建 pdf。看我的编辑 1
  • 什么是文件中的文件路径...我没有任何文件,而我只有 out.pdf...
  • 我之前也实现了相同的...但是显示错误“文档路径无效”..
  • 它不起作用,因为我在任何路径中都没有文件...并且针对没有 SD 卡或没有空间的设备...所以内容提供商似乎是唯一的解决方案...跨度>
【解决方案2】:

当在应用程序中点击 openwith 时.. 我正在使用内容提供商,因为我的文件在物理上不可用...

 try {
            createCachedFile(this, attachmentFileName, bytes);
            startActivity(pdfOpenWith(this, attachmentFileName, bytes));
        }catch (IOException e) {
            e.printStackTrace();
        } catch (ActivityNotFoundException e) {
            Toast.makeText(this, "Not available on this device.", Toast.LENGTH_SHORT).show();
        }

public  void createCachedFile(Context context, String fileName, byte[] content) throws IOException {
    File cacheFile = new File(context.getCacheDir() + File.separator + fileName);
    cacheFile.createNewFile();
    BufferedOutputStream bostream = new BufferedOutputStream(new FileOutputStream(cacheFile));
    bostream.write(content);
    bostream.flush();
    bostream.close();
}

public static Intent pdfOpenWith(Activity context, String fileName, byte[] bytecontent)
{
    Intent pdfintent = new Intent(Intent.ACTION_VIEW);
    //pdfintent.setType("application/pdf");
    Uri contenturi = Uri.parse("content://" + AttachFileProvider.AUTHORITY + "/" + fileName);
    Toast.makeText(context, fileName, Toast.LENGTH_LONG).show ();
    pdfintent.setDataAndType(contenturi, "application/pdf");
    pdfintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    pdfintent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    return pdfintent;

【讨论】:

    猜你喜欢
    • 2012-12-05
    • 2012-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-05
    • 1970-01-01
    • 2012-11-03
    • 1970-01-01
    相关资源
    最近更新 更多