title: Pkpass on Android
body: <p>There is an android app, Passwallet, that is able to interpret pkpass files intented for the apple app Passbook (<a href="https://play.google.com/store/apps/details?id=com.attidomobile.passwallet" rel="noreferrer">https://play.google.com/store/apps/details?id=com.attidomobile.passwallet</a>)</p>
  
  <p>I was wondering how to read pkpass files.</p>
  
  <p>Pkpass files seem to be zip files with all the information inside in json files. Is there a default structure for the pkpass files? If so what is it? And what would be a good way to import that into a android app?</p>
  
  <hr>
  
  <p>For the people who wonder how read the content of the pkpass file refer to the following code:</p>
  
  <p>I set up this activity with the intent filter for pkpass files</p>
  
  <pre><code>        &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.VIEW" /&gt;
  
              &lt;category android:name="android.intent.category.DEFAULT" /&gt;
  
              &lt;data
                  android:mimeType="application/vnd-com.apple.pkpass"
                  android:scheme="content" /&gt;
          &lt;/intent-filter&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.VIEW" /&gt;
  
              &lt;category android:name="android.intent.category.DEFAULT" /&gt;
  
              &lt;data
                  android:mimeType="application/vnd.apple.pkpass"
                  android:scheme="content" /&gt;
          &lt;/intent-filter&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.VIEW" /&gt;
  
              &lt;category android:name="android.intent.category.DEFAULT" /&gt;
  
              &lt;data
                  android:mimeType="application/vnd-com.apple.pkpass"
                  android:scheme="file" /&gt;
          &lt;/intent-filter&gt;
          &lt;intent-filter&gt;
              &lt;action android:name="android.intent.action.VIEW" /&gt;
  
              &lt;category android:name="android.intent.category.DEFAULT" /&gt;
  
              &lt;data
                  android:mimeType="application/vnd.apple.pkpass"
                  android:scheme="file" /&gt;
          &lt;/intent-filter&gt;
  </code></pre>
  
  <hr>
  
  <pre><code>@Override
  protected void onCreate(Bundle savedInstanceState) {
  
  super.onCreate(savedInstanceState);
  Intent intent = getIntent();
  Uri uri = intent.getData();
  String scheme = uri.getScheme();
  
  if(ContentResolver.SCHEME_CONTENT.equals(scheme)) {
      try {
          InputStream attachment = getContentResolver().openInputStream(uri);
          handleZipInput(attachment);
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      }
  }
  else {
      String path = uri.getEncodedPath();
      try {
          FileInputStream fis = new FileInputStream(path);
          handleZipInput(fis);
      } catch (FileNotFoundException e) {
          e.printStackTrace();
      }
  }
  }
  
  private void handleZipInput(InputStream in) {
      try {
          ZipInputStream zis = new ZipInputStream(in);
          ZipEntry entry;
          while((entry = zis.getNextEntry()) != null) {
              String filename = entry.getName();
              if(filename.equals("pass.json")) {
                  StringBuilder s = new StringBuilder();
                  int read = 0;
                  byte[] buffer = new byte[1024];
                  while((read = zis.read(buffer, 0, 1024)) &gt;= 0)
                      s.append(new String(buffer, 0, read));
  
                  JSONObject pass = new JSONObject(s.toString());
                  break;
              }
          }
      } catch (Exception e) {
          e.printStackTrace();
      }
  }
  </code></pre>