Пример #1
0
  private File getFileFromUri(Uri uri) throws FileNotFoundException {
    Cursor cursor = null;
    String filename = null;
    File videoDir = null;

    int match = getUriMatcher().match(uri);
    Log.d("getFileFromUri(uri=" + uri + ", match=" + match + ")");
    switch (match) {
      case RESOURCES_ID:
        cursor =
            query(
                uri,
                new String[] {Resources._ID, Resources.DIRECTORY, Resources.FILENAME},
                null,
                null,
                null);
        if (!cursor.moveToFirst()) {
          cursor.close();
          throw new FileNotFoundException("Could not find Resource for uri");
        }
        filename = cursor.getString(cursor.getColumnIndex(Resources.FILENAME));
        videoDir = directorySwitch(cursor.getString(cursor.getColumnIndex(Resources.DIRECTORY)));
        Log.d("Got filename: " + filename + " directory: " + videoDir);
        break;
      case DOWNLOADS_ID:
        cursor =
            query(
                uri,
                new String[] {Downloads._ID, Downloads.DIRECTORY, Downloads.FILENAME},
                null,
                null,
                null);
        if (!cursor.moveToFirst()) {
          cursor.close();
          throw new FileNotFoundException("Could not find Download for uri");
        }
        filename = cursor.getString(cursor.getColumnIndex(Downloads.FILENAME));
        videoDir = directorySwitch(cursor.getString(cursor.getColumnIndex(Downloads.DIRECTORY)));
        Log.d("Got filename: " + filename + " directory: " + videoDir);
        break;
      case RESOURCES_THUMBNAILS:
        Log.d("Got thumbnail: " + uri);
        filename = uri.getLastPathSegment();
        File thumbnailDirectory = VizUtils.getVideosThumbnailDir();
        Log.d("Full thumbnail directory path: " + VizUtils.getVideosThumbnailPath());

        if (thumbnailDirectory == null || !thumbnailDirectory.exists()) {
          Log.e("Could not create directory error: " + VizUtils.getVideosThumbnailPath());
          throw new FileNotFoundException("Media not mounted error: thumbnail could not be found");
        }

        Log.d("Got thumbnail filename: " + filename);

        return new File(thumbnailDirectory, filename);
      default:
        throw new FileNotFoundException("No case for " + match);
    }

    cursor.close();
    if (videoDir == null) {
      throw new FileNotFoundException("no video directory specified for " + match);
    }
    if (filename == null) {
      throw new FileNotFoundException("no filename specified for " + match);
    }
    return new File(videoDir, filename);
  }