code: public static File fileFromKey(SQLDatabase db, byte[] key, String attachmentsDir,
                              boolean allowCreateName)
              throws AttachmentException {
  
          String keyString = keyToString(key);
          String filename = null;
  
          db.beginTransaction();
          Cursor c = null;
          try {
              c = db.rawQuery(SQL_FILENAME_LOOKUP_QUERY, new String[]{ keyString });
              if (c.moveToFirst()) {
                  filename = c.getString(0);
                  logger.finest(String.format("Found filename %s for key %s", filename, keyString));
              } else if (allowCreateName) {
                  filename = generateFilenameForKey(db, keyString);
                  logger.finest(String.format("Added filename %s for key %s", filename, keyString));
              }
              db.setTransactionSuccessful();
          } catch (SQLException e) {
              logger.log(Level.WARNING, "Couldn't read key,filename mapping database", e);
              filename = null;
          } finally {
              DatabaseUtils.closeCursorQuietly(c);
              db.endTransaction();
          }
  
          if (filename != null) {
              return new File(attachmentsDir, filename);
          } else {
              // generateFilenameForKey throws an exception if we couldn't generate, this
              // means we couldn't get one from the database.
              throw new AttachmentException("Couldn't retrieve filename for attachment");
          }
      }
docs: Lookup or create a on disk File representation of blob, in {@code db} using {@code key}.
  
  Existing attachments will have an entry in db, so the method just looks this up
  and returns the File object for the path.
  
  For new attachments, the {@code key} doesn't already have an associated filename,
  one is generated and inserted into the database before returning a File object
  for blob associated with {@code key}.
  
  @param db database to use.
  @param key key to lookup filename for.
  @param attachmentsDir Root directory for attachment blobs.
  @param allowCreateName if the is no existing mapping, whether one should be created
  and returned. If false, and no existing mapping, AttachmentException
  is thrown.
  @return File object for blob associated with {@code key}.
  @throws AttachmentException if a mapping doesn't exist and {@code allowCreateName} is
  false or if the name generation process fails.