2012-10-15 13 views
7

Czy ktoś ma pomysł, jak otworzyć plik PDF w systemie Android? Mój kod wygląda to w ten sposób:Jak otworzyć plik PDF w Androidzie z folderu zasobów?

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

Można spojrzeć na ten link: http://stackoverflow.com/questions/6491210/how-to-open-a-pdf-stored-either-in-res-raw-or-assets-folder – omi0301

+0

sprawdzić to http: // stackoverflow.com/questions/6491210/how-to-open-a-pdf-stored-either-in-res-raw-lub-assets- folder – Aamirkhan

+0

@sai proszę określić, jaki problem napotkasz, tj. jakikolwiek wyjątek/błąd, czy aplikacja wyświetla listę dostępnych przeglądarek PDF? –

Odpowiedz

2

Oto kod do otwierania pliku PDF z folderu aktywów, ale trzeba mieć czytnik PDF zainstalowane na urządzeniu:

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

Ta odpowiedź jest zasadniczo skopiowana dosłownie z tego: http://stackoverflow.com/questions/17085574/read-a-pdf-file-from-assets-folder – csvan

Powiązane problemy