android.database.Cursor类的使用及代码示例

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

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

Cursor介绍

[英]This interface provides random read-write access to the result set returned by a database query.

Cursor implementations are not required to be synchronized so code using a Cursor from multiple threads should perform its own synchronization when using the Cursor.
[中]此接口提供对数据库查询返回的结果集的随机读写访问。
游标实现不需要同步,因此使用来自多个线程的游标的代码在使用游标时应该执行自己的同步。

代码示例

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

protected void onActivityResult(int requestCode, int resultCode, 
    Intent imageReturnedIntent) {
  super.onActivityResult(requestCode, resultCode, imageReturnedIntent); 

  switch(requestCode) { 
  case REQ_CODE_PICK_IMAGE:
    if(resultCode == RESULT_OK){  
      Uri selectedImage = imageReturnedIntent.getData();
      String[] filePathColumn = {MediaStore.Images.Media.DATA};

      Cursor cursor = getContentResolver().query(
                selectedImage, filePathColumn, null, null, null);
      cursor.moveToFirst();

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
      String filePath = cursor.getString(columnIndex);
      cursor.close();

      Bitmap yourSelectedImage = BitmapFactory.decodeFile(filePath);
    }
  }
}

代码示例来源:origin: bumptech/glide

private List<MediaStoreData> query(Uri contentUri, String[] projection, String sortByCol,
  String idCol, String dateTakenCol, String dateModifiedCol, String mimeTypeCol,
  String orientationCol, MediaStoreData.Type type) {
 final List<MediaStoreData> data = new ArrayList<MediaStoreData>();
 Cursor cursor = getContext().getContentResolver()
   .query(contentUri, projection, null, null, sortByCol + " DESC");
 if (cursor == null) {
  return data;
 }
 try {
  final int idColNum = cursor.getColumnIndexOrThrow(idCol);
  final int dateTakenColNum = cursor.getColumnIndexOrThrow(dateTakenCol);
  final int dateModifiedColNum = cursor.getColumnIndexOrThrow(dateModifiedCol);
  final int mimeTypeColNum = cursor.getColumnIndex(mimeTypeCol);
  final int orientationColNum = cursor.getColumnIndexOrThrow(orientationCol);
  while (cursor.moveToNext()) {
   long id = cursor.getLong(idColNum);
   long dateTaken = cursor.getLong(dateTakenColNum);
   String mimeType = cursor.getString(mimeTypeColNum);
   long dateModified = cursor.getLong(dateModifiedColNum);
   int orientation = cursor.getInt(orientationColNum);
   data.add(new MediaStoreData(id, Uri.withAppendedPath(contentUri, Long.toString(id)),
     mimeType, dateTaken, dateModified, orientation, type));
  }
 } finally {
  cursor.close();
 }
 return data;
}

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

Cursor cursor = db.rawQuery(...);
try {
  while (cursor.moveToNext()) {
    ...
  }
} finally {
  cursor.close();
}

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

String selection = "_id = "+id;
Uri uri = Uri.parse("content://sms");
Cursor cursor = contentResolver.query(uri, null, selection, null, null);
String phone = cursor.getString(cursor.getColumnIndex("address"));
int type = cursor.getInt(cursor.getColumnIndex("type"));// 2 = sent, etc.
String date = cursor.getString(cursor.getColumnIndex("date"));
String body = cursor.getString(cursor.getColumnIndex("body"));

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

Intent intent = new Intent();
      intent.setType("image/*");
      intent.setAction(Intent.ACTION_GET_CONTENT);
      startActivityForResult(Intent.createChooser(intent,
          "Select Picture"), SELECT_PICTURE);
if( cursor != null ){
  int column_index = cursor
  .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
  cursor.moveToFirst();
  String path = cursor.getString(column_index);
  cursor.close();
  return path;
return uri.getPath();

代码示例来源:origin: TommyLemon/APIJSON

/**根据图库图片uri发送图片
 * @param selectedImage
 */
private void sendPicByUri(Uri selectedImage) {
  Cursor cursor = getContentResolver().query(selectedImage, null, null, null, null);
  if (cursor != null) {
    cursor.moveToFirst();
    int columnIndex = cursor.getColumnIndex("_data");
    picturePath = cursor.getString(columnIndex);
    cursor.close();
    cursor = null;
    if (picturePath == null || picturePath.equals("null")) {
      Toast toast = Toast.makeText(this, "找不到图片", Toast.LENGTH_SHORT);
      toast.setGravity(Gravity.CENTER, 0, 0);
      toast.show();
      return;
    }
  } else {
    File file = new File(selectedImage.getPath());
    if (!file.exists()) {
      Toast toast = Toast.makeText(this, "找不到图片", Toast.LENGTH_SHORT);
      toast.setGravity(Gravity.CENTER, 0, 0);
      toast.show();
      return;
    }
    picturePath = file.getAbsolutePath();
  }
  setResult(RESULT_OK, new Intent().putExtra(RESULT_PICTURE_PATH, picturePath));
}

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

@SuppressWarnings("deprecation")
Cursor imagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null, null, orderBy);
int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
this.count = imagecursor.getCount();
this.arrPath = new String[this.count];
ids = new int[count];
this.thumbnailsselection = new boolean[this.count];
for (int i = 0; i < this.count; i++) {
  imagecursor.moveToPosition(i);
  ids[i] = imagecursor.getInt(image_column_index);
  int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
  arrPath[i] = imagecursor.getString(dataColumnIndex);
imagecursor.close();
btnSelect.setOnClickListener(new OnClickListener() {
      Intent i = new Intent();
      i.putExtra("data", selectImages);
      setResult(Activity.RESULT_OK, i);
  protected void onPostExecute(Bitmap result) {
    super.onPostExecute(result);
    iv.setImageBitmap(result);

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

Intent intent = new Intent();
mImageView = (ImageView) findViewById(R.id.iv_photo);
button.setOnClickListener(new View.OnClickListener() {
  @Override
  public void onClick(View v) {
  String path = mImageCaptureUri.getPath();
  Log.i("TAG",
      "!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!After capture path "
  Bundle extras = data.getExtras();
  selectedImagePath = mImageCaptureUri.getPath();
    mImageView.setImageBitmap(bm);
  BitmapFactory.Options o = new BitmapFactory.Options();
  o.inJustDecodeBounds = true;
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: stackoverflow.com

.getColumnIndexOrThrow(ExcercisesDbAdapter.KEY_ROWID);
Cursor exercisesCursor = mDbHelper
        .fetchExcercisesForGroup(groupCursor
                .getLong(mGroupIdColumnIndex));
startManagingCursor(exercisesCursor);
return exercisesCursor;
details.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
        Long exerciseId = exerciseCursor.getLong(exerciseCursor.getColumnIndex(ExcercisesDbAdapter.KEY_ROWID));
        Intent i = new Intent(ExercisesList.this, ExerciseView.class);
        i.putExtra(ExcercisesDbAdapter.KEY_ROWID, exerciseId);
        startActivity(i);

代码示例来源:origin: LawnchairLauncher/Lawnchair

@Override
public void onReceive(Context context, Intent intent) {
  DownloadManager downloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
  String action = intent.getAction();
  // We only want to check if the download has completed
  if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(action)) {
    DownloadManager.Query query = new DownloadManager.Query();
    query.setFilterById(downloadId);
    // Check if file has been successfully downloaded
    Cursor c = downloadManager.query(query);
    if (c.moveToFirst()) {
      int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_STATUS);
      int status = c.getInt(columnIndex);
      switch (status) {
        // If everything is fine, call the abstract method and proceed
        case DownloadManager.STATUS_SUCCESSFUL:
          Uri uri = Uri.parse(c.getString(c.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI)));
          onDownloadDone(uri);
          break;
        // Otherwise tell the user that the download failed
        default:
          Toast.makeText(context, R.string.download_file_error, Toast.LENGTH_LONG);
          break;
      }
      context.unregisterReceiver(this);
    }
    // Close any open resources
    c.close();
  }
}

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

public void sendTheMessage(View v) {
  Cursor c=(Cursor)contacts.getSelectedItem();
  
  if (means.getCheckedRadioButtonId()==R.id.client) {
   Intent sms=new Intent(Intent.ACTION_SENDTO,
              Uri.parse("smsto:"+c.getString(2)));
   
   sms.putExtra("sms_body", msg.getText().toString());
   
   startActivity(sms);
  }
  else {
   SmsManager
    .getDefault()
    .sendTextMessage(c.getString(2), null,
             msg.getText().toString(),
             null, null);
  }
 }
}

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

public void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  if (resultCode == RESULT_OK) {
    switch(requestCode) {
    case SELECT_IMAGE:
      mSelectedImagePath = getPath(data.getData());
      break;
  }
}

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: stackoverflow.com

img_logo.setOnClickListener(new OnClickListener() {
  public void onClick(View v) {
    startDialog();
        File f = new File(android.os.Environment
            .getExternalStorageDirectory(), "temp.jpg");
        intent.putExtra(MediaStore.EXTRA_OUTPUT,
            Uri.fromFile(f));
    img_logo.setImageBitmap(bitmap);
  if (data != null) {
    Uri selectedImage = data.getData();
    String[] filePath = { MediaStore.Images.Media.DATA };
    Cursor c = getContentResolver().query(selectedImage, filePath,
        null, null, null);
    c.moveToFirst();
    int columnIndex = c.getColumnIndex(filePath[0]);
    selectedImagePath = c.getString(columnIndex);
    c.close();
    img_logo.setImageBitmap(bitmap);

代码示例来源:origin: north2016/T-MVP

public static String getUrlByIntent(Context mContext, Intent mdata) {
  Uri uri = mdata.getData();
  String scheme = uri.getScheme();
  String data = "";
  if (scheme == null)
    data = uri.getPath();
  else if (ContentResolver.SCHEME_FILE.equals(scheme)) {
    data = uri.getPath();
  } else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
    Cursor cursor = mContext.getContentResolver().query(uri,
        new String[]{MediaStore.Images.ImageColumns.DATA},
        null, null, null);
    if (null != cursor) {
      if (cursor.moveToFirst()) {
        int index = cursor.getColumnIndex(
            MediaStore.Images.ImageColumns.DATA);
        if (index > -1) {
          data = cursor.getString(index);
        }
      }
      cursor.close();
    }
  }
  return data;
}

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

public static ArtistInfo getArtistinfo(Context context, long id) {
  ContentResolver cr = context.getContentResolver();
  Cursor cursor = cr.query(MediaStore.Audio.Artists.EXTERNAL_CONTENT_URI, proj_artist, "_id =" + String.valueOf(id), null, null);
  if (cursor == null) {
    return null;
  }
  ArtistInfo artistInfo = new ArtistInfo();
  while (cursor.moveToNext()) {
    artistInfo.artist_name = cursor.getString(cursor.getColumnIndex(MediaStore.Audio.Artists.ARTIST));
    artistInfo.number_of_tracks = cursor.getInt(cursor.getColumnIndex(MediaStore.Audio.Artists.NUMBER_OF_TRACKS));
  }
  cursor.close();
  return artistInfo;
}

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

imagepath = Environment.getExternalStorageDirectory()+"/sharedresources/"+HelperFunctions.getDateTimeForFileName()+".png";
  uriImagePath = Uri.fromFile(new File(imagepath));
  photoPickerIntent.setType("image/*");
  photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT,uriImagePath);
  photoPickerIntent.putExtra("outputFormat",Bitmap.CompressFormat.PNG.name());
  photoPickerIntent.putExtra("return-data", true);
  startActivityForResult(photoPickerIntent, REQUEST_CODE_CHOOSE_PICTURE_FROM_GALLARY);
            copyFile(new File(getRealPathFromURI(data.getData())), f);
          } catch (IOException e) {
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);

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

Bitmap photo = (Bitmap) data.getExtras().get("data"); 
  imageView.setImageBitmap(photo);
  knop.setVisibility(Button.VISIBLE);
  Uri tempUri = getImageUri(getApplicationContext(), photo);
  File finalFile = new File(getRealPathFromURI(tempUri));
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
cursor.moveToFirst(); 
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
return cursor.getString(idx);

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

private int getmCardId() {
  final ContentResolver resolver = getContentResolver();
  Cursor cursor = resolver.query(Uri.parse("content://media/external/fs_id"), null, null,
      null, null);
  int mCardId = -1;
  if (cursor != null && cursor.moveToFirst()) {
    mCardId = cursor.getInt(0);
    cursor.close();
    cursor = null;
  }
  return mCardId;
}

相关文章