Android - ouvrir le pdf dans une application externe
j'ai un fichier pdf dans mon appli actifs répertoire que je veux l'ouvrir à l'aide d'une application, comme l'écrit le contenu de mon fournisseur et je suis tryng pour le faire fonctionner, mais rien...
voici le code:
Fournisseur De Contenu:
package package.name;
import java.io.File;
import java.io.FileNotFoundException;
import java.net.URI;
import android.content.ContentProvider;
import android.content.ContentValues;
import android.database.Cursor;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
public class FileContentProvider extends ContentProvider {
private static final String URI_PREFIX = "content://package.name.filecontentprovider";
public static String constructUri(String url) {
Uri uri = Uri.parse(url);
return uri.isAbsolute() ? url : URI_PREFIX + url;
}
@Override
public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
URI uri1 = URI.create("file:///data/data/package.name/"+uri.getPath());
File file = new File(uri1.getPath());
ParcelFileDescriptor parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
return parcel;
}
@Override
public boolean onCreate() {
return true;
}
@Override
public int delete(Uri uri, String s, String[] as) {
throw new UnsupportedOperationException("Not supported by this provider");
}
@Override
public String getType(Uri uri) {
throw new UnsupportedOperationException("Not supported by this provider");
}
@Override
public Uri insert(Uri uri, ContentValues contentvalues) {
throw new UnsupportedOperationException("Not supported by this provider");
}
@Override
public Cursor query(Uri uri, String[] as, String s, String[] as1, String s1) {
throw new UnsupportedOperationException("Not supported by this provider");
}
@Override
public int update(Uri uri, ContentValues contentvalues, String s, String[] as) {
throw new UnsupportedOperationException("Not supported by this provider");
}
}
et voici comment je l'appelle de l'ouverture du fichier:
File pdf = new File("assets/prova.pdf");
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse("content://package.name/" + pdf));
i.setType("application/pdf");
startActivity(i);
j'ai ajouté la ligne suivante dans le manifeste android, à l'intérieur de la balise:
<provider android:name=".FileContentProvider" android:authorities="package.name" />
c'est le logcat de sortie:
02-26 19:47:44.938: ERROR/AndroidRuntime(6494): Uncaught handler: thread main exiting due to uncaught exception
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.tf.thinkdroid.samsung/com.tf.thinkdroid.pdf.app.PdfRenderScreen}: java.lang.NullPointerException
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at android.os.Handler.dispatchMessage(Handler.java:99)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at android.os.Looper.loop(Looper.java:123)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at android.app.ActivityThread.main(ActivityThread.java:4363)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at java.lang.reflect.Method.invokeNative(Native Method)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at java.lang.reflect.Method.invoke(Method.java:521)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:862)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at dalvik.system.NativeStart.main(Native Method)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): Caused by: java.lang.NullPointerException
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at com.tf.thinkdroid.pdf.app.RenderScreen.onNewIntent(Unknown Source)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at com.tf.thinkdroid.pdf.app.RenderScreen.onCreate(Unknown Source)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at com.tf.thinkdroid.pdf.app.PdfRenderScreen.onCreate(Unknown Source)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
02-26 19:47:44.953: ERROR/AndroidRuntime(6494): ... 11 more
ne comprends pas d'où le problème est, semble que l'application externe ne peut pas obtenir le fichier.
merci pour toute aide!
OriginalL'auteur Marco Faion | 2011-02-26
Vous devez vous connecter pour publier un commentaire.
Je suis nouveau sur le développement android et a passé toute la journée à la recherche pour la raison que mon stockés localement PDF ne peut pas être ouvert par les applications externes. Je suis heureux d'avoir trouvé ce fil.
En attendant Marco ai eu de travail et décrit ici.
Attention: la langue italienne - service de traduction de Google pourrait aider 😉
http://www.marcofaion.it/?p=7http://web.archive.org/web/20111020204554/http://www.marcofaion.it/?p=7
Notes supplémentaires à son howto pour les débutants:
La ligne de Marco mentions à insérer dans le Manifest.xml
doit être inséré dans la
<application ...></application>
tag.Et si vous voulez des noms de fichier que vous devez échanger
avec
Les fichiers PDF doivent être mis dans le dossier déjà existant actifs dans votre projet (surtout pas dans tout nouvellement ajouté dossier res/actifs ou de qqch.)! =)
OriginalL'auteur radioking
Mon application est ci-dessous. Notez que si votre nom de fichier est MyPdf.pdf, le fichier doit être actif/public_pdfs/MyPdf.pdf.mp3. Le chemin public_pdfs est à seulement exporter les fichiers pdf que vous voulez vraiment à l'exportation, et l' .mp3 extension est pour éviter de compression.
AndroidManifest.xml
L'ouverture d'un PDF
PdfContentProvider.java
Semble un peu cassé. Seule chose qui arrive, c'est le Toast avec le message "Ce document ne peut pas être ouvert"...
OriginalL'auteur Cristan
setType()
réinitialise votreUri
ànull
. Essayez de sauter leUri
dans le constructeur et en utilisantsetDataAndType()
à la place.OriginalL'auteur CommonsWare