Friday, September 26, 2014

Android app PDF package & open


contoh:  buku aqidah-manhaj.pdf

letak di folder assets
pengguna akan memperolehi salinan aqidah-manhaj.pdf di sdcard.
Buka guna PDF.Reader untuk baca, tanda bookmark, salin teks.

App ini akan membuka PDF yang sama.  Jadi pengguna akses dokumen yang sama setiap kali, data disimpan.

Aqidah Muslimin (Islam)
https://play.google.com/store/apps/details?id=com.azharawang.aqidah







public void pdf8(View v)
    {
        File fileBrochure = new File(Environment.getExternalStorageDirectory().getPath() + "/aqidah-manhaj.pdf");
   if (!fileBrochure.exists())
   {
        CopyAssetsbrochure8();
   }

   /** PDF reader code */
   File file = new File(Environment.getExternalStorageDirectory().getPath() + "/aqidah-manhaj.pdf");      

   Intent intent = new Intent(Intent.ACTION_VIEW);
   intent.setDataAndType(Uri.fromFile(file),"application/pdf");
   intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
   intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
   try
   {
       getApplicationContext().startActivity(intent);
   }
   catch (ActivityNotFoundException e)
   {
      //  Toast.makeText(SecondActivity.this, "NO Pdf Viewer",

//Toast.LENGTH_SHORT).show();
   }
}

//method to write the PDFs file to sd card
   private void CopyAssetsbrochure8() {
       AssetManager assetManager = getAssets();
       String[] files = null;
       try
       {
           files = assetManager.list("");
       }
       catch (IOException e)
       {
           Log.e("tag", e.getMessage());
       }
       for(int i=0; i<files.length; i++)
       {
           String fStr = files[i];
           if(fStr.equalsIgnoreCase("aqidah-manhaj.pdf"))
           {
               InputStream in = null;
               OutputStream out = null;
               try
               {
                 in = assetManager.open(files[i]);
                 out = new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + "/" + files[i]);
                 copyFile8(in, out);
                 in.close();
                 in = null;
                 out.flush();
                 out.close();
                 out = null;
                 break;
               }
               catch(Exception e)
               {
                   Log.e("tag", e.getMessage());
               }
           }
       }
   }

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