android.database.Cursor.getColumnIndexOrThrow()方法的使用及代码示例

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

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

Cursor.getColumnIndexOrThrow介绍

[英]Returns the zero-based index for the given column name, or throws IllegalArgumentException if the column doesn't exist. If you're not sure if a column will exist or not use #getColumnIndex(String) and check for -1, which is more efficient than catching the exceptions.
[中]返回给定列名的从零开始的索引,如果该列不存在,则抛出IllegalArgumentException。如果不确定列是否存在,请使用#getColumnIndex(String)并检查-1,这比捕获异常更有效。

代码示例

代码示例来源: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: naman14/Timber

public String getAlbumArtistName() {
  synchronized (this) {
    if (mAlbumCursor == null) {
      return null;
    }
    return mAlbumCursor.getString(mAlbumCursor.getColumnIndexOrThrow(AlbumColumns.ARTIST));
  }
}

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

String[] projection = { MediaStore.Images.Media.DATA}; 
     Cursor cursor = managedQuery(mCapturedImageURI, projection, null, null, null); 
     int column_index_data = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     String capturedImageFilePath = cursor.getString(column_index_data);

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

Data.CONTACT_ID + " ASC");
int idIndex = contacts.getColumnIndexOrThrow(Contacts._ID);
int nameIndex = contacts.getColumnIndexOrThrow(Contacts.DISPLAY_NAME);
int cidIndex = data.getColumnIndexOrThrow(Data.CONTACT_ID);
int data1Index = data.getColumnIndexOrThrow(Data.DATA1);
boolean hasData = data.moveToNext();
while (contacts.moveToNext()) {
  long id = contacts.getLong(idIndex);
  System.out.println("Contact(" + id + "): " + contacts.getString(nameIndex));
  if (hasData) {
    long cid = data.getLong(cidIndex);
      if (cid == id) {
        System.out.println("\t(" + cid + "/" + id + ").data1:" + 
            data.getString(data1Index));
      hasData = data.moveToNext();
      if (hasData) {
        cid = data.getLong(cidIndex);

代码示例来源: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: LitePalFramework/LitePal

protected long getForeignKeyValue(String tableWithFK, String tableWithoutFK, long id) {
  Cursor cursor = Connector.getDatabase().query(tableWithFK, null, "id = ?",
      new String[]{String.valueOf(id)}, null, null, null);
  long foreignKeyId = 0;
  if (cursor.moveToFirst()) {
    foreignKeyId = cursor.getLong(cursor.getColumnIndexOrThrow(BaseUtility
        .changeCase(tableWithoutFK + "_id")));
  }
  cursor.close();
  return foreignKeyId;
}

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

private static void cleanupPlaylist(final Context context, final long playlistId,
                  final Cursor cursor) {
  final int idCol = cursor.getColumnIndexOrThrow(MediaStore.Audio.Playlists.Members.AUDIO_ID);
  final Uri uri = MediaStore.Audio.Playlists.Members.getContentUri("external", playlistId);
  ArrayList<ContentProviderOperation> ops = new ArrayList<ContentProviderOperation>();
  ops.add(ContentProviderOperation.newDelete(uri).build());
  final int YIELD_FREQUENCY = 100;
  if (cursor.moveToFirst() && cursor.getCount() > 0) {
    do {
      final ContentProviderOperation.Builder builder =
          ContentProviderOperation.newInsert(uri)
              .withValue(Playlists.Members.PLAY_ORDER, cursor.getPosition())
              .withValue(Playlists.Members.AUDIO_ID, cursor.getLong(idCol));
      if ((cursor.getPosition() + 1) % YIELD_FREQUENCY == 0) {
        builder.withYieldAllowed(true);
      }
      ops.add(builder.build());
    } while (cursor.moveToNext());
  }
  try {
    context.getContentResolver().applyBatch(MediaStore.AUTHORITY, ops);
  } catch (RemoteException e) {
  } catch (OperationApplicationException e) {
  }
}

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

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

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

public String getRealPathFromURI(Uri contentUri) {
   String[] proj = { MediaStore.Images.Media.DATA };
   Cursor cursor = managedQuery(contentUri, proj, null, null, null);
   int column_index = cursor
       .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
   cursor.moveToFirst();
   return cursor.getString(column_index);
 }

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

public String getArtistName() {
  synchronized (this) {
    if (mCursor == null) {
      return null;
    }
    return mCursor.getString(mCursor.getColumnIndexOrThrow(AudioColumns.ARTIST));
  }
}

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

null, null);
column_index_data = cursor.getColumnIndexOrThrow(MediaColumns.DATA);
column_index_folder_name = cursor
    .getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME);
while (cursor.moveToNext()) {
  absolutePathOfImage = cursor.getString(column_index_data);

代码示例来源:origin: greenrobot/greenDAO

public void testCursorQuerySimple() {
  insert(3);
  CursorQuery query = dao.queryBuilder().orderAsc(Properties.SimpleInteger).buildCursor();
  Cursor cursor = query.query();
  try {
    assertEquals(3, cursor.getCount());
    assertTrue(cursor.moveToNext());
    int columnIndex = cursor.getColumnIndexOrThrow(Properties.SimpleInteger.columnName);
    assertEquals(getSimpleInteger(0), cursor.getInt(columnIndex));
    assertTrue(cursor.moveToNext());
    assertEquals(getSimpleInteger(1), cursor.getInt(columnIndex));
    assertTrue(cursor.moveToNext());
    assertEquals(getSimpleInteger(2), cursor.getInt(columnIndex));
    assertFalse(cursor.moveToNext());
  } finally {
    cursor.close();
  }
}

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

private String getRealPathFromURI(Uri contentUri) {
  String[] proj = { MediaStore.Images.Media.DATA };
  CursorLoader loader = new CursorLoader(mContext, contentUri, proj, null, null, null);
  Cursor cursor = loader.loadInBackground();
  int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  cursor.moveToFirst();
  String result = cursor.getString(column_index);
  cursor.close();
  return result;
}

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

public String getPath(Uri uri) {
  String[] projection = { MediaStore.Images.Media.DATA };
  Cursor cursor = managedQuery(uri, projection, null, null, null);
  startManagingCursor(cursor);
  int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  cursor.moveToFirst();
  return cursor.getString(column_index);
}

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

public String getPath() {
  synchronized (this) {
    if (mCursor == null) {
      return null;
    }
    return mCursor.getString(mCursor.getColumnIndexOrThrow(AudioColumns.DATA));
  }
}

代码示例来源:origin: jeasonlzy/ImagePicker

if (data != null) {
  ArrayList<ImageItem> allImages = new ArrayList<>();   //所有图片的集合,不分文件夹
  while (data.moveToNext()) {
    String imageName = data.getString(data.getColumnIndexOrThrow(IMAGE_PROJECTION[0]));
    String imagePath = data.getString(data.getColumnIndexOrThrow(IMAGE_PROJECTION[1]));
    long imageSize = data.getLong(data.getColumnIndexOrThrow(IMAGE_PROJECTION[2]));
    int imageWidth = data.getInt(data.getColumnIndexOrThrow(IMAGE_PROJECTION[3]));
    int imageHeight = data.getInt(data.getColumnIndexOrThrow(IMAGE_PROJECTION[4]));
    String imageMimeType = data.getString(data.getColumnIndexOrThrow(IMAGE_PROJECTION[5]));
    long imageAddTime = data.getLong(data.getColumnIndexOrThrow(IMAGE_PROJECTION[6]));

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

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

  return phoneContactID;
}

代码示例来源: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: stackoverflow.com

public String getRealPathFromURI(Uri contentUri) {          
String [] proj={MediaColumns.DATA};  
Cursor cursor = managedQuery( contentUri,  
    proj, // Which columns to return  
    null,       // WHERE clause; which rows to return (all rows)  
    null,       // WHERE clause selection arguments (none)  
    null); // Order-by clause (ascending by name)  
int column_index = cursor.getColumnIndexOrThrow(MediaColumns.DATA);  
cursor.moveToFirst();  
return cursor.getString(column_index);}

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

public String getAlbumName() {
  synchronized (this) {
    if (mCursor == null) {
      return null;
    }
    return mCursor.getString(mCursor.getColumnIndexOrThrow(AudioColumns.ALBUM));
  }
}

相关文章