android.content.Context.getContentResolver()方法的使用及代码示例

x33g5p2x  于2022-01-17 转载在 其他  
字(10.2k)|赞(0)|评价(0)|浏览(453)

本文整理了Java中android.content.Context.getContentResolver()方法的一些代码示例,展示了Context.getContentResolver()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。Context.getContentResolver()方法的具体详情如下:
包路径:android.content.Context
类名称:Context
方法名:getContentResolver

Context.getContentResolver介绍

暂无

代码示例

代码示例来源:origin: stackoverflow.com

public String getRealPathFromURI(Context context, Uri contentUri) {
 Cursor cursor = null;
 try { 
  String[] proj = { MediaStore.Images.Media.DATA };
  cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
  int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  cursor.moveToFirst();
  return cursor.getString(column_index);
 } finally {
  if (cursor != null) {
   cursor.close();
  }
 }
}

代码示例来源:origin: square/picasso

@Override
protected int getExifOrientation(Uri uri) {
 Cursor cursor = null;
 try {
  ContentResolver contentResolver = context.getContentResolver();
  cursor = contentResolver.query(uri, CONTENT_ORIENTATION, null, null, null);
  if (cursor == null || !cursor.moveToFirst()) {
   return 0;
  }
  return cursor.getInt(0);
 } catch (RuntimeException ignored) {
  // If the orientation column doesn't exist, assume no rotation.
  return 0;
 } finally {
  if (cursor != null) {
   cursor.close();
  }
 }
}

代码示例来源:origin: stackoverflow.com

public static int getOrientation(Context context, Uri photoUri) {
  /* it's on the external media. */
  Cursor cursor = context.getContentResolver().query(photoUri,
      new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);

  if (cursor.getCount() != 1) {
    return -1;
  }

  cursor.moveToFirst();
  return cursor.getInt(0);
}

代码示例来源:origin: stackoverflow.com

public static int getContactIDFromNumber(String contactNumber,Context context)
{
  contactNumber = Uri.encode(contactNumber);
  int phoneContactID = new Random().nextInt();
  Cursor contactLookupCursor = context.getContentResolver().query(Uri.withAppendedPath(PhoneLookup.CONTENT_FILTER_URI,contactNumber),new String[] {PhoneLookup.DISPLAY_NAME, PhoneLookup._ID}, null, null, null);
    while(contactLookupCursor.moveToNext()){
      phoneContactID = contactLookupCursor.getInt(contactLookupCursor.getColumnIndexOrThrow(PhoneLookup._ID));
      }
    contactLookupCursor.close();

  return phoneContactID;
}

代码示例来源:origin: commonsguy/cw-omnibus

void bindModel(Cursor row) {
 title.setText(row.getString(row.getColumnIndex(MediaStore.Video.Media.TITLE)));
 int uriColumn=row.getColumnIndex(MediaStore.Video.Media.DATA);
 int mimeTypeColumn=
   row.getColumnIndex(MediaStore.Video.Media.MIME_TYPE);
 int videoId=row.getInt(row.getColumnIndex(MediaStore.Video.Media._ID));
 videoUri=row.getString(uriColumn);
 videoMimeType=row.getString(mimeTypeColumn);
  ContentResolver cr=thumbnail.getContext().getContentResolver();
  BitmapFactory.Options options=new BitmapFactory.Options();

代码示例来源:origin: stackoverflow.com

public static Uri getImageContentUri(Context context, File imageFile) {
   String filePath = imageFile.getAbsolutePath();
   Cursor cursor = context.getContentResolver().query(
       MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
       new String[] { MediaStore.Images.Media._ID },
       MediaStore.Images.Media.DATA + "=? ",
       new String[] { filePath }, null);
   if (cursor != null && cursor.moveToFirst()) {
     int id = cursor.getInt(cursor
         .getColumnIndex(MediaStore.MediaColumns._ID));
     Uri baseUri = Uri.parse("content://media/external/images/media");
     return Uri.withAppendedPath(baseUri, "" + id);
   } else {
     if (imageFile.exists()) {
       ContentValues values = new ContentValues();
       values.put(MediaStore.Images.Media.DATA, filePath);
       return context.getContentResolver().insert(
           MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
     } else {
       return null;
     }
   }
 }

代码示例来源:origin: naman14/Timber

public static final long[] getSongListForArtist(final Context context, final long id) {
  final String[] projection = new String[]{
      BaseColumns._ID
  };
  final String selection = MediaStore.Audio.AudioColumns.ARTIST_ID + "=" + id + " AND "
      + MediaStore.Audio.AudioColumns.IS_MUSIC + "=1";
  Cursor cursor = context.getContentResolver().query(
      MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, selection, null,
      MediaStore.Audio.AudioColumns.ALBUM_KEY + "," + MediaStore.Audio.AudioColumns.TRACK);
  if (cursor != null) {
    final long[] mList = SongLoader.getSongListForCursor(cursor);
    cursor.close();
    cursor = null;
    return mList;
  }
  return sEmptyList;
}

代码示例来源:origin: mayubao/KuaiChuan

Cursor cursor = context.getContentResolver().query(fileUri, projection, selection, null, sortOrder);
if(cursor != null){
  while (cursor.moveToNext()){
    try{
      String data = cursor.getString(0);
      FileInfo fileInfo = new FileInfo();
      fileInfo.setFilePath(data);

代码示例来源:origin: ACRA/acra

@NonNull
public static String getFileNameFromUri(@NonNull Context context, @NonNull Uri uri) throws FileNotFoundException {
  try (Cursor cursor = context.getContentResolver().query(uri, new String[]{OpenableColumns.DISPLAY_NAME}, null, null, null)) {
    if (cursor != null && cursor.moveToFirst()) {
      return cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
    }
  }
  throw new FileNotFoundException("Could not resolve filename of " + uri);
}

代码示例来源:origin: cats-oss/android-gpuimage

@Override
  protected int getImageOrientation() throws IOException {
    Cursor cursor = context.getContentResolver().query(uri,
        new String[]{MediaStore.Images.ImageColumns.ORIENTATION}, null, null, null);
    if (cursor == null || cursor.getCount() != 1) {
      return 0;
    }
    cursor.moveToFirst();
    int orientation = cursor.getInt(0);
    cursor.close();
    return orientation;
  }
}

代码示例来源:origin: naman14/Timber

public static final long[] getSongListForAlbum(final Context context, final long id) {
  final String[] projection = new String[]{
      BaseColumns._ID
  };
  final String selection = MediaStore.Audio.AudioColumns.ALBUM_ID + "=" + id + " AND " + MediaStore.Audio.AudioColumns.IS_MUSIC
      + "=1";
  Cursor cursor = context.getContentResolver().query(
      MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projection, selection, null,
      MediaStore.Audio.AudioColumns.TRACK + ", " + MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
  if (cursor != null) {
    final long[] mList = SongLoader.getSongListForCursor(cursor);
    cursor.close();
    cursor = null;
    return mList;
  }
  return sEmptyList;
}

代码示例来源:origin: stackoverflow.com

public static String getPath(Context context, Uri uri) throws URISyntaxException {
  if ("content".equalsIgnoreCase(uri.getScheme())) {
    String[] projection = { "_data" };
    Cursor cursor = null;

    try {
      cursor = context.getContentResolver().query(uri, projection, null, null, null);
      int column_index = cursor.getColumnIndexOrThrow("_data");
      if (cursor.moveToFirst()) {
        return cursor.getString(column_index);
      }
    } catch (Exception e) {
      // Eat it
    }
  }
  else if ("file".equalsIgnoreCase(uri.getScheme())) {
    return uri.getPath();
  }

  return null;
}

代码示例来源:origin: stackoverflow.com

public boolean contactExists(Context context, String number) {
/// number is the phone number
Uri lookupUri = Uri.withAppendedPath(
PhoneLookup.CONTENT_FILTER_URI, 
Uri.encode(number));
String[] mPhoneNumberProjection = { PhoneLookup._ID, PhoneLookup.NUMBER, PhoneLookup.DISPLAY_NAME };
Cursor cur = context.getContentResolver().query(lookupUri,mPhoneNumberProjection, null, null, null);
try {
  if (cur.moveToFirst()) {
   return true;
}
} finally {
if (cur != null)
  cur.close();
}
return false;
}

代码示例来源:origin: naman14/Timber

public static Song getSongFromPath(String songPath, Context context) {
  ContentResolver cr = context.getContentResolver();
  Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
  String selection = MediaStore.Audio.Media.DATA;
  String[] selectionArgs = {songPath};
  String[] projection = new String[]{"_id", "title", "artist", "album", "duration", "track", "artist_id", "album_id"};
  String sortOrder = MediaStore.Audio.Media.TITLE + " ASC";
  Cursor cursor = cr.query(uri, projection, selection + "=?", selectionArgs, sortOrder);
  if (cursor != null && cursor.getCount() > 0) {
    Song song = getSongForCursor(cursor);
    cursor.close();
    return song;
  }
  else return new Song();
}

代码示例来源:origin: naman14/Timber

private String getValueForDownloadedFile(Context context, Uri uri, String column) {
  Cursor cursor = null;
  final String[] projection = {
      column
  };
  try {
    cursor = context.getContentResolver().query(uri, projection, null, null, null);
    if (cursor != null && cursor.moveToFirst()) {
      return cursor.getString(0);
    }
  } finally {
    if (cursor != null) {
      cursor.close();
    }
  }
  return null;
}

代码示例来源:origin: naman14/Timber

public static final int getSongCountForPlaylist(final Context context, final long playlistId) {
  Cursor c = context.getContentResolver().query(
      MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId),
      new String[]{BaseColumns._ID}, MUSIC_ONLY_SELECTION, null, null);
  if (c != null) {
    int count = 0;
    if (c.moveToFirst()) {
      count = c.getCount();
    }
    c.close();
    c = null;
    return count;
  }
  return 0;
}

代码示例来源:origin: aa112901/remusic

private String getValueForDownloadedFile(Context context, Uri uri, String column) {
  Cursor cursor = null;
  final String[] projection = {
      column
  };
  try {
    cursor = context.getContentResolver().query(uri, projection, null, null, null);
    if (cursor != null && cursor.moveToFirst()) {
      return cursor.getString(0);
    }
  } finally {
    if (cursor != null) {
      cursor.close();
    }
  }
  return null;
}

代码示例来源:origin: naman14/Timber

public static final int getSongCountForAlbumInt(final Context context, final long id) {
  int songCount = 0;
  if (id == -1) {
    return songCount;
  }
  Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, id);
  Cursor cursor = context.getContentResolver().query(uri,
      new String[]{MediaStore.Audio.AlbumColumns.NUMBER_OF_SONGS}, null, null, null);
  if (cursor != null) {
    cursor.moveToFirst();
    if (!cursor.isAfterLast()) {
      if (!cursor.isNull(0)) {
        songCount = cursor.getInt(0);
      }
    }
    cursor.close();
    cursor = null;
  }
  return songCount;
}

代码示例来源:origin: Justson/AgentWeb

static String getDataColumn(Context context, Uri uri, String selection, String[] selectionArgs) {
  Cursor cursor = null;
  String[] projection = {MediaStore.Images.Media.DATA};
  try {
    cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, null);
    if (cursor != null && cursor.moveToFirst()) {
      int index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
      return cursor.getString(index);
    }
  } finally {
    if (cursor != null) {
      cursor.close();
    }
  }
  return null;
}

代码示例来源:origin: aa112901/remusic

public static final int getSongCountForAlbumInt(final Context context, final long id) {
  int songCount = 0;
  if (id == -1) {
    return songCount;
  }
  Uri uri = ContentUris.withAppendedId(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, id);
  Cursor cursor = context.getContentResolver().query(uri,
      new String[]{MediaStore.Audio.AlbumColumns.NUMBER_OF_SONGS}, null, null, null);
  if (cursor != null) {
    cursor.moveToFirst();
    if (!cursor.isAfterLast()) {
      if (!cursor.isNull(0)) {
        songCount = cursor.getInt(0);
      }
    }
    cursor.close();
    cursor = null;
  }
  return songCount;
}

相关文章

微信公众号

最新文章

更多

Context类方法