Je développe une application Android et je dois ouvrir des fichiers.
Ceci est mon code en utilisant l'intention:
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);
}
}
Le fichier se trouve dans le répertoire racine de la carte SD et je peux l’ouvrir manuellement.
Problème
L'application est fermée lorsqu'elle arrive sur startActivity (intent). Je pense que le problème est dans le fichier AndroidManifest.xml, mais je ne sais pas comment le mettre correctement.
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)
Pouvez-vous m'aider à compléter AndroidManifest? Ou comment puis-je ouvrir ce pdf?
Le problème est qu'aucune application n'est installée pour gérer l'ouverture du fichier PDF. Vous devez utiliser le sélecteur d'intention, comme suit:
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
}
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();
}
Version Kotlin ci-dessous (Version mise à jour de la réponse @ paul-burke:
fun openPDFDocument(context: Context, filename: String) {
//Create PDF Intent
val pdfFile = File(Environment.getExternalStorageDirectory().absolutePath + "/" + filename)
val pdfIntent = Intent(Intent.ACTION_VIEW)
pdfIntent.setDataAndType(Uri.fromFile(pdfFile), "application/pdf")
pdfIntent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
//Create Viewer Intent
val viewerIntent = Intent.createChooser(pdfIntent, "Open PDF")
context.startActivity(viewerIntent)
}