<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.myapplication"> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> <application android:allowBackup="false" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:roundIcon="@mipmap/ic_launcher_round" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.VIEW" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest>
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/openPdf" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Open PDF" android:layout_centerInParent="true"/> </RelativeLayout>
package com.example.myapplication; import android.Manifest; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.Environment; import android.os.StrictMode; import android.support.v4.app.ActivityCompat; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; import android.view.View; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.lang.reflect.Method; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { /* Workaround pour le pb URI (PAS PROPRE et surement inutile si tu n'es pas sur Nougat ou plus) */ try{ Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure"); m.invoke(null); }catch(Exception e){ e.printStackTrace(); } super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); /* clic sur le bouton */ findViewById(R.id.openPdf).setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Context c = getApplicationContext(); try { File fileInDataDir = copyFileFromAssetsToDownloads(c, "", "file.pdf"); Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(fileInDataDir), "application/pdf"); intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); startActivity(intent); } catch(Exception e) { Log.e(getClass().getSimpleName(), "Exception: " + e.getMessage(), e); } } }); /* Permissions Nougat et +*/ ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.READ_EXTERNAL_STORAGE, }, 1); } private static File copyFileFromAssetsToDownloads(final Context context, final String assetFolder, final String fileName) throws IOException { /* utilisation du répertoire download pour pour que l'application externe puisse lire le fichier sinon ça semble coincer. */ File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS) + "/" + fileName); if(f.exists()) //noinspection ResultOfMethodCallIgnored f.delete(); InputStream in = null; FileOutputStream out = null; try { in = context.getAssets().open(assetFolder + fileName); out = new FileOutputStream(f); int read; final byte[] buffer = new byte[4096]; while ((read = in.read(buffer)) > 0) out.write(buffer, 0, read); } catch(IOException ioe) { throw new IOException(ioe); } finally { if(out != null) out.close(); if(in != null) in.close(); } return f; } }
Context c = getApplicationContext(); try { InputStreamReader isr = new InputStreamReader(c.getAssets().open("fichier.pdf")); } catch(IOException ie) { Log.e(getClass().getSimpleName(), "IOException: " + ie.getMessage(), ie); }
private static File copyFileFromAssets(final Context context, final String assetFolder, final String fileName) throws PackageManager.NameNotFoundException, IOException { final PackageManager m = context.getPackageManager(); String newPath = context.getPackageName(); final PackageInfo p = m.getPackageInfo(newPath, 0); newPath = p.applicationInfo.dataDir + "/" + fileName; File f = new File(newPath); if(f.exists()) //noinspection ResultOfMethodCallIgnored f.delete(); InputStream in = null; FileOutputStream out = null; try { in = context.getAssets().open(assetFolder + fileName); out = new FileOutputStream(f); int read; final byte[] buffer = new byte[4096]; while ((read = in.read(buffer)) > 0) out.write(buffer, 0, read); } catch(IOException ioe) { throw new IOException(ioe); } finally { if(out != null) out.close(); if(in != null) in.close(); } return f; } /* utilisation*/ Context c = getApplicationContext(); try { File fileInDataDir = copyFileFromAssets(c /* Context */, "" /* le fichier pourrait être dans le un sous répertoire, ex: foo dans ce cas il faudra mettre 'foo/' */, "fichier.pdf" /* fichier dans l'assets */); } catch(Exception e) { Log.e(getClass().getSimpleName(), "Exception: " + e.getMessage(), e); }
Context c = getApplicationContext(); try { InputStreamReader isr = new InputStreamReader(c.getAssets().open("fichier.pdf")); } catch(IOException ie) { Log.e(getClass().getSimpleName(), "IOException: " + ie.getMessage(), ie); }
//Au clic sur ce Layout, que le téléchargement du fichier .pdf soit démarré automatique LinearLayout afficher_lecture_pdf = (LinearLayout) findViewById(R.id.lecture_pdf); afficher_lecture_pdf.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { Context c = getApplicationContext(); try { InputStreamReader isr = new InputStreamReader(c.getAssets().open("fichier.pdf")); } catch(IOException ie) { Log.e(getClass().getSimpleName(), "IOException: " + ie.getMessage(), ie); } } });
InputStreamReader isr = new InputStreamReader(c.getAssets().open("fichier.pdf"));
File pdf = // fichier PDF après la copie depuis l'assets Intent intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(Uri.fromFile(pdf), "application/pdf"); intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY); startActivity(intent);
Modifié le 23 oct. 2018 à 23:23
Tu m'as sauvé. ) :
24 oct. 2018 à 08:35
Pense à valider le sujet si ton problème est résolu.
++
24 oct. 2018 à 16:33
J'ai validé depuis ma boîte mail en cliquant sur < Votre question est résolue > plusieurs fois, mais, ça ne donne toujours aucun résultat.
Merci pour tout