2019년 3월 14일 목요일

[Android] Image query on MediaStore.

Android MediaStore class supports all available media and meta data on internal/external storage devices.

Android Image information can be found in MediaStore.Images.Media


Content Provier can be access MediaStore class with query method.
Refer ContentProvider

public Cursor query (Uri uri,
                String[] projection, 
                String selection, 
                String[] selectionArgs, 
                String sortOrder)

For example, if images query on external storage, then

void imageQuery(Context context) {
    Cursor imageCursor = null;
    // projection parameters    
    String[] imageColumns = {MediaStore.Images.Media._ID,
MediaStore.Images.Media.DATA,
                             MediaStore.Images.Media.ORIENTATION,
MediaStore.Images.Media.WIDTH,
                             MediaStore.Images.Media.HEIGHT,
MediaStore.Images.Media.DATE_ADDED};

    imageCursor = context.getContentResolver().query(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                             imageColumns, 
null, // ignore selection.
                             null, // ignore selectionArgs.
null); // ignore sort order.

    if (imageCursor != null && imageCursor.moveToFirst()) {
        int imgIdColumnIndex = imageCursor.getColumnIndex(MediaStore.Images.Media._ID);
        int imgPathColumnIndex = imageCursor.getColumnIndex(MediaStore.Images.Media.DATA);
int imgOrientationColumnIndex = imageCursor.getColumnIndex(MediaStore.Images.Media.ORIENTATION);
        int imgWidthColumnIndex = imageCursor.getColumnIndex(MediaStore.Images.Media.WIDTH);
int imgHeightColumnIndex = imageCursor.getColumnIndex(MediaStore.Images.Media.HEIGHT);
        int dateAddedColumnIndex = imageCursor.getColumnIndex(MediaStore.Images.Media.DATE_ADDED);
        do {
            int imgId = imageCursor.getInt(imgIdColumnIndex);
            string imgPath = imageCursor.getString(imgPathColumnIndex);
            int orientation = imageCursor.getInt(imgOrientationColumnIndex);
            int width = imageCursor.getInt(imgWidthColumnIndex);
            int height = imageCursor.getInt(imgHeightColumnIndex);
            int dateAdded = imageCursor.getInt(dateAddedColumnIndex);
            // do action.
        } while(imageCursor.moveToNext());
    }
}

After querying images, image columns index get from image cursor column index.
Each image information can get from current image cursor by moving image cursor.

This example gets the image id, path, orientation, width, height, added date from MediaStore.
After received image data, it can be used to load, show and crop image etc.



댓글 없음:

댓글 쓰기