MediaStore.Images.Media无法显示所有图像,解决方案

时间:2014-09-22 14:31:24

标签: android android-contentprovider android-gallery mediastore

使用MediaStore.Images.Media进行查询不显示所有图像是查询内部内容还是外部内容

结果(缩略图和从中检索缩略图的位置)在主流设备中并不相同。没有SD卡且只有内部存储设备有不同的图像效果,三星设备有不同的结果,谷歌设备有不同的结果。

具体来说,这个代码在stackoverflow中用作解决方案,

            final String[] columns = {MediaStore.Images.Thumbnails._ID};
            final String orderBy = MediaStore.Images.Media._ID;
            Cursor imagecursor = context.getContentResolver().query(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
                    null, null, orderBy);
            if (imagecursor != null) {
                int image_column_index = imagecursor
                        .getColumnIndex(MediaStore.Images.Media._ID);
                int count = imagecursor.getCount();
                for (int i = 0; i < count; i++) {
                    imagecursor.moveToPosition(i);
                    int id = imagecursor.getInt(image_column_index);
                    ImageItem imageItem = new ImageItem();
                    imageItem.id = id;
                    lastId = id;
                    imageItem.img = MediaStore.Images.Thumbnails.getThumbnail(
                            context.getApplicationContext().getContentResolver(), id,
                            MediaStore.Images.Thumbnails.MINI_KIND, null);
                    images.add(imageItem);
                }
                imagecursor.close();

我看一下代码,看起来似乎很合理,但是我需要一个更好的解决方案,因为结果会有所不同,而且在某些设备上我不知道从哪里得到缩略图

1 个答案:

答案 0 :(得分:2)

正如@ danny117向我指出的那样,试图获得THUMBNAILS是一个有缺陷的假设。人们不能依赖于android系统中每个图像的缩略图的存在。

所以我最终根据mime类型检索所有图像,并使用Files MediaStore

 String[] projection = {
            MediaStore.Files.FileColumns._ID,
            MediaStore.Files.FileColumns.DATA,
            MediaStore.Files.FileColumns.DATE_ADDED,
            MediaStore.Files.FileColumns.MEDIA_TYPE,
            MediaStore.Files.FileColumns.MIME_TYPE,
            MediaStore.Files.FileColumns.TITLE
    };

    // Return only video and image metadata.
    String selection = MediaStore.Files.FileColumns.MEDIA_TYPE + "="
            + MediaStore.Files.FileColumns.MEDIA_TYPE_IMAGE
            + " OR "
            + MediaStore.Files.FileColumns.MEDIA_TYPE + "="
            + MediaStore.Files.FileColumns.MEDIA_TYPE_VIDEO;

    Uri queryUri = MediaStore.Files.getContentUri("external");

    CursorLoader cursorLoader = new CursorLoader(
            getActivity(),
            queryUri,
            projection,
            selection,
            null, // Selection args (none).
            MediaStore.Files.FileColumns.DATE_ADDED + " DESC" // Sort order.
    );


    images.clear();
    /*
        final String[] columns = {MediaStore.Images.ImageColumns._ID, MediaStore.Images.ImageColumns.DATE_ADDED};
        final String orderBy = MediaStore.Images.Media.DATE_ADDED + " DESC";
        Cursor imagecursor = getActivity().getContentResolver().query(
            MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,
            null, null, orderBy);
    */

    Cursor imagecursor = cursorLoader.loadInBackground();

    if (imagecursor != null) {
        int image_column_index = imagecursor
                .getColumnIndex(MediaStore.Files.FileColumns._ID);
        int type_column_index = imagecursor.getColumnIndex(MediaStore.Files.FileColumns.MIME_TYPE);
        int count = imagecursor.getCount();
        for (int i = 0; i < count; i++) {
            imagecursor.moveToPosition(i);
            int id = imagecursor.getInt(image_column_index);
            String mime_type = imagecursor.getString(type_column_index);
            ImageItem imageItem = new ImageItem();
            imageItem.id = id;
            //lastId = id;

            if(!mime_type.contains("video"))
                imageItem.uriString = MediaStore.Images.Media.EXTERNAL_CONTENT_URI.withAppendedPath(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, String.valueOf(id)).toString();
            else
                imageItem.uriString = MediaStore.Video.Media.EXTERNAL_CONTENT_URI.withAppendedPath(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, String.valueOf(id)).toString();



            images.add(imageItem);
        }

        //add ImageItem at top of list

        imagecursor.close();
    }