2011-06-27 8 views
1

私のアプリケーションにresource/raw/color_chart_ciao.pdfというPDFファイルがあります。そのファイルを自分のアプリケーションに表示したいと思います。私はそのためのコードを書かれている:PDFをアンドロイドアプリに表示

File file = new File("http://www.adobe.com/devnet/acrobat/pdfs/pdf_open_parameters.pdf"); 
     System.out.println("FIle Path is" + file); 
     if (file.exists()) { 
      System.out.println("FIle Path is" + file); 
      Uri path = Uri.fromFile(file); 
      Intent intent = new Intent(Intent.ACTION_VIEW); 
      intent.setDataAndType(path, "application/pdf"); 
      intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP); 

      try { 
       startActivity(intent); 
       System.out.println("pdf show"); 
      } 
      catch (ActivityNotFoundException e) { 
       Toast.makeText(CiaoView.this, 
        "No Application Available to View PDF", 
        Toast.LENGTH_SHORT).show(); 
      } 
     } 

が、私は自分のアプリケーションを実行すると、私は自分のアプリケーションでPDFを見ることができません。 私はアプリケーション開発にもっと忙しいです。だからこの質問で助けてください。あなたは、デバイス/エミュレータにインストールされ、デフォルトのPDFリーダーを持っている場合

答えて

0

は、あなただけが意図を通じてPDFを開くことができますが、あなたのアプリケーションは、単独アプリケーションではありません。アプリケーションをスタンドアローンにしたい場合は、アプリケーションにPDFリーダーを実装する必要があります。 オープンソースプロジェクトPDFがあります。あなたの活動に次のコード - コピー

  1. apdf
  2. mupdf
+0

おかげで非常に多くのu – user642347

0

:ここ

PDFのためのいくつかのリンクです。必要な場所からCopyReadAssets( "File_name.pdf")関数を呼び出します。 File_name.pdfファイルをassetsフォルダに配置します。それは動作します!ハッピーコーディング! :)

private void CopyReadAssets(String pdfname) 
{ 
AssetManager assetManager = getAssets(); 
InputStream in = null; 
OutputStream out = null; 
File file = new File(getFilesDir(), pdfname); 
try 
{ 
    in = assetManager.open(pdfname); 
    out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE); 
    copyFile(in, out); 
    in.close(); 
    in = null; 
    out.flush(); 
    out.close(); 
    out = null; 
} catch (Exception e) 
{ 
    Toast.makeText(getApplicationContext(), "Pdf Viewer not installed", Toast.LENGTH_SHORT).show(); 
} 
try 
{ 
Intent intent = new Intent(Intent.ACTION_VIEW); 
intent.setDataAndType(
     Uri.parse("file://" + getFilesDir() + "/"+pdfname), 
     "application/pdf"); 

startActivity(intent); 
}catch (Exception e) { 
    // TODO: handle exception 
    Toast.makeText(getApplicationContext(), "Pdf Viewer not installed" ,Toast.LENGTH_SHORT).show(); 
} 

}