본문 바로가기

Study/Android

미디어 스토어 사진 가져오는 쿼리

private Cursor getImageMediaCursor(Context context, int nSort, int limit, int offset) {
	Cursor mPictureCursor;
	String[] proj = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DATA, MediaStore.Images.Media.DATE_MODIFIED, MediaStore.Images.Media.DATE_TAKEN, MediaStore.Images.Media.SIZE, MediaStore.Images.Media.DISPLAY_NAME, MediaStore.Images.Media.MIME_TYPE };

	if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
	{
		Bundle bundle = new Bundle();
		bundle.putInt(ContentResolver.QUERY_ARG_LIMIT, limit);
		bundle.putInt(ContentResolver.QUERY_ARG_OFFSET, offset);
		bundle.putString(ContentResolver.QUERY_ARG_SORT_COLUMNS, MediaStore.Images.Media.DATE_TAKEN);
		bundle.putString(ContentResolver.QUERY_ARG_SORT_COLUMNS, MediaStore.Images.Media.DATE_MODIFIED);
		bundle.putInt(ContentResolver.QUERY_ARG_SORT_DIRECTION, ContentResolver.QUERY_SORT_DIRECTION_DESCENDING);

		mPictureCursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj, bundle, null);
	} else {
		String sort = MediaStore.Images.Media.DATE_TAKEN + " desc ," + MediaStore.Images.Media.DATE_MODIFIED + " desc LIMIT " + limit + " OFFSET " + offset;
		mPictureCursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, proj, null, null, sort);
	}
	return mPictureCursor;
}

 

 

fun setData() {
    val loading = false // 로딩 중이면 쿼리를 실행하지 않음
    val list = functionSearchPictureFileItemList(this, true, 0, mLimit, mOffset)
    val adapter = GalleryAdapter(this, this, this, applicationContext)
    adapter.setData(list)
    binding.gridGallery.setScrollListener { view, firstVisibleItem, visibleItemCount, totalItemCount ->
        if (!loading && (totalItemCount - visibleItemCount) <= (firstVisibleItem)) {
            loading = true
            mOffset += mLimit

            val getData = functionSearchPictureFileItemList(this, true, 0, mLimit, mOffset)
            list.addAll(getData)
            adapter.setData(list)
            adapter.notifyDataSetChanged()
        } else {
            loading = false
        }
    }
    binding.gridGallery.adapter = adapter
}