2012-10-15 21 views
7

Qualcuno ha un'idea su come aprire un file PDF in Android? Il mio codice è presente questo:Come aprire il file PDF in Android dalla cartella delle risorse?

public class SampleActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     CopyReadAssets();  
    } 

    private void CopyReadAssets() { 
     AssetManager assetManager = getAssets(); 

     InputStream in = null; 
     OutputStream out = null; 
     File file = new File(getFilesDir(), "git.pdf"); 
     try { 
      in = assetManager.open("git.pdf"); 
      out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE); 

      copyFile(in, out); 
      in.close(); 
      in = null; 
      out.flush(); 
      out.close(); 
      out = null; 
     } catch (Exception e) { 
      Log.e("tag", e.getMessage()); 
     } 

     Intent intent = new Intent(Intent.ACTION_VIEW); 
     intent.setDataAndType(
       Uri.parse("file://" + getFilesDir() + "/git.pdf"), 
       "application/pdf"); 

     startActivity(intent); 
    } 

    private void copyFile(InputStream in, OutputStream out) throws IOException { 
     byte[] buffer = new byte[1024]; 
     int read; 
     while ((read = in.read(buffer)) != -1) { 
      out.write(buffer, 0, read); 
     } 
    } 
} 
+0

Si può guardare a questo link: http://stackoverflow.com/questions/6491210/how-to-open-a-pdf-stored-either-in-res-raw-or-assets-folder – omi0301

+0

controllare questo http: // stackoverflow.com/questions/6491210/how-to-open-a-pdf-stored-either-in-res-raw-or-assets- cartella – Aamirkhan

+0

@sai si prega di specificare quale problema si deve affrontare, ad esempio qualsiasi eccezione/errore, o l'app visualizza l'elenco degli elenchi di PDF Viewer disponibili? –

risposta

2

Ecco il codice per l'apertura di file PDF dalla cartella risorsa, ma è necessario avere lettore PDF installato sul dispositivo:

private void CopyAssets() { 

     AssetManager assetManager = getAssets(); 

     InputStream in = null; 
     OutputStream out = null; 
     File file = new File(getFilesDir(), "fileName.pdf"); 
     try { 
      in = assetManager.open("fileName.pdf"); 
      out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE); 

      copyFile(in, out); 
      in.close(); 
      in = null; 
      out.flush(); 
      out.close(); 
      out = null; 
     } catch (Exception e) { 
      Log.e("tag", e.getMessage()); 
     } 

     Intent intent = new Intent(Intent.ACTION_VIEW); 
     intent.setDataAndType(
       Uri.parse("file://" + getFilesDir() + "/fileName.pdf"), 
       "application/pdf"); 

     startActivity(intent); 
    } 

    private void copyFile(InputStream in, OutputStream out) throws IOException { 
     byte[] buffer = new byte[1024]; 
     int read; 
     while ((read = in.read(buffer)) != -1) { 
      out.write(buffer, 0, read); 
     } 
    } 
+3

Questa risposta è essenzialmente copiata letteralmente da questa: http://stackoverflow.com/questions/17085574/lettura-a-PDF-file-da-asset-cartella – csvan

Problemi correlati