簡體   English   中英

打開帶有openwith選項android的pdf

[英]open pdf with openwith option android

我用自己的應用程序打開了pdf。 現在當用戶點擊按鈕時,我顯示openwith選項application / pdf。 現在用戶選擇他的選擇(例如adobe reader),打開的pdf文件必須顯示在用戶選擇中(在這種情況下為adobereader)。 對於打開的PDF,我有bytearray和輸入流。

試試這個

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.call createPDF()然后打開OpenPDF()。

這是工作代碼。

當在應用程序中點擊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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM