示例#1
0
  /** Remotely opens a file */
  @Override
  public ParcelFileDescriptor openFile(Uri uri, String mode) throws FileNotFoundException {
    if (Constants.LOGVV) {
      logVerboseOpenFileInfo(uri, mode);
    }

    Cursor cursor = query(uri, new String[] {"_data"}, null, null, null);
    String path;
    try {
      int count = (cursor != null) ? cursor.getCount() : 0;
      if (count != 1) {
        // If there is not exactly one result, throw an appropriate
        // exception.
        if (count == 0) {
          throw new FileNotFoundException("No entry for " + uri);
        }
        throw new FileNotFoundException("Multiple items at " + uri);
      }

      cursor.moveToFirst();
      path = cursor.getString(0);
    } finally {
      if (cursor != null) {
        cursor.close();
      }
    }

    if (path == null) {
      throw new FileNotFoundException("No filename found.");
    }
    if (!Helpers.isFilenameValid(path)) {
      throw new FileNotFoundException("Invalid filename.");
    }
    if (!"r".equals(mode)) {
      throw new FileNotFoundException("Bad mode for " + uri + ": " + mode);
    }

    ParcelFileDescriptor ret =
        ParcelFileDescriptor.open(new File(path), ParcelFileDescriptor.MODE_READ_ONLY);

    if (ret == null) {
      if (Constants.LOGV) {
        Log.v(Constants.TAG, "couldn't open file");
      }
      throw new FileNotFoundException("couldn't open file");
    }
    return ret;
  }