2013-07-03 13 views
22

私はAndroidアプリケーションを開発中で、いくつかのファイルを開く必要があります。Androidオープンpdfファイル

これは意図を使用して私のコードです:

public class FacturaActivity extends Activity { 

    (...) 

    public void downloadInvoice(View view) { 
     File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ filename); 
     Intent intent = new Intent(Intent.ACTION_VIEW); 
     intent.setDataAndType(Uri.fromFile(file),"application/pdf"); 
     intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); 
     startActivity(intent); 
    } 
} 

ファイルは、SDカードのルートディレクトリにあると私はそれを手動で開くことができます。それはstartActivity(意図)に到着したときに

問題

アプリケーションが閉じられています。問題はAndroidManifest.xmlファイルにあると思いますが、正しく配置する方法はわかりません。

のAndroidManifest.xml

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

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="8" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" 
    android:name="###.MyApplication" > <!--cant show complete name--> 
    <activity 
     android:name="###.MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <activity 
     android:name=".FacturaActivity" > 
    </activity> 

</application> 

LogCat

07-03 15:49:13.094: E/AndroidRuntime(1032): FATAL EXCEPTION: main 
07-03 15:49:13.094: E/AndroidRuntime(1032): java.lang.IllegalStateException: Could not execute method of the activity 
(...) 
07-03 15:49:13.094: E/AndroidRuntime(1032): Caused by: android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/201209_F2012212782.PDF typ=application/pdf flg=0x40000000 } 
07-03 15:49:13.094: E/AndroidRuntime(1032):  at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408) 
07-03 15:49:13.094: E/AndroidRuntime(1032):  at android.app.Instrumentation.execStartActivity(Instrumentation.java:1378) 
07-03 15:49:13.094: E/AndroidRuntime(1032):  at android.app.Activity.startActivityForResult(Activity.java:2817) 
07-03 15:49:13.094: E/AndroidRuntime(1032):  at android.app.Activity.startActivity(Activity.java:2923) 

あなたはAndroidManifestを完了するために私を助けることができますか?または、どうすればそのpdfを開くことができますか?

+0

あなたのAndroidはありません任意のPDFリーダーアプリがインストールされているようだ、私はThinkFreeのPDFビューアで他のPDFのファイルを開くことができます1 –

+0

を追加します。 – Lyd

+0

このブログ記事は、あなたが 'ContentProvider'を使用しようとしている場合に役立ちます(今推奨されています):http://www.blogc.at/2014/03/23/share-private-files-with-other-apps- fileprovider/ – Sakiboy

答えて

69

問題は、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 
} 
+0

私は自分の携帯電話で試しました。別のアプリのpdfを開くと、ThinkFreeを登録すると言われます。「後で」と「Android用ThinkFree PDF Viewer Mobile」が開かれ、pdfファイルが表示されます。あなたのコードでは、ThinkFreeの登録を求めるだけです。私が「後で」と言うと、それは閉じます。 – Lyd

+1

これはこのコードとは関係ありません。 ThinkFreeがこの意図を処理する方法を制御することはできません。別のPDFビューアをインストールしてみてください。 –

+0

ありがとう、それは大丈夫です。もう1つ...私はPDFファイルを閉じると(電話の戻るボタンで)私のアプリケーションはエラーで終了します。それをどうすれば解決できますか? – Lyd

5
String dir="/Attendancesystem"; 

public void displaypdf() { 

     File file = null; 
      file = new File(Environment.getExternalStorageDirectory()+dir+ "/sample.pdf"); 
     Toast.makeText(getApplicationContext(), file.toString() , Toast.LENGTH_LONG).show(); 
     if(file.exists()) { 
      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 
      } 
     } 
     else 
      Toast.makeText(getApplicationContext(), "File path is incorrect." , Toast.LENGTH_LONG).show(); 
    } 
+0

ファイルが内部ストレージに保存されている場合は、上記のコードを使用してください。 –

関連する問題