2013-07-03 18 views
22

Sto sviluppando un'applicazione Android e devo aprire alcuni file.file PDF aperto Android

Questo è il mio codice utilizzando intento:

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

file si trova nella directory principale della scheda SD e posso aprire manualmente.

Problema

applicazione viene chiusa quando arriva a startActivity (intenti). Penso che il problema sia nel file AndroidManifest.xml, ma non so come inserirlo correttamente.

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) 

Potete aiutarmi a completare AndroidManifest? O come posso aprire quel pdf?

+0

Sembra che il tuo Android ha installato alcuna applicazione qualsiasi lettore PDF, aggiungere un –

+0

posso aprire i file di altri PDF con ThinkFree PDF Viewer. – Lyd

+0

Questo post del blog aiuta se stai cercando di usare 'ContentProvider' (che ora è raccomandato): http://www.blogc.at/2014/03/23/share-private-files-with-other-apps- fileprovider/ – Sakiboy

risposta

69

Il problema è che non è stata installata alcuna app per gestire l'apertura del PDF. Si dovrebbe usare Scelta Intent, in questo modo:

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

L'ho provato nel mio cellulare. Quando apro del pdf di un'altra app, si dice di registrare ThinkFree, si dice "Later" e "ThinkFree PDF Viewer Mobile per Android" è aperto, mostrandomi il file pdf. Con il tuo codice, arriva solo per chiedere la registrazione a ThinkFree. Se dico "Più tardi", si chiude. – Lyd

+1

Questo non ha nulla a che fare con questo codice. Non è possibile controllare il modo in cui ThinkFree gestisce questo intento. Prova ad installare un visualizzatore PDF alternativo. –

+0

Grazie, va bene. Un'altra cosa ... Quando chiudo il file pdf (con il pulsante Indietro del telefono) la mia applicazione si chiude con un errore. Come posso risolverlo? – 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

Usa il codice sopra, quando il tuo file è memorizzato nella memoria interna. –