Android之通过ContentResolver获取手机图片和视频的路径和生成缩略图和缩略图路径

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/u011068702/article/details/83628654

1 问题

获取手机所有图片和视频的路径和生成图片和视频的缩略图和缩略图路径

生成缩略图我们用的系统函数

            public static Bitmap getThumbnail(ContentResolver cr, long origId, int kind, Options options) {
                throw new RuntimeException("Stub!");
            }

调用如下

MediaStore.Images.Thumbnails.getThumbnail

2 获取手机所有视频测试代码

    /**
     *get system orignal Thumbnail
     **/
    private String getVideoSystemThumbnail(Content content, String id) {
        String thumbnail = "";
        ContentResolver cr = content.getContentResolver();
        Cursor cursor = cr.query(
                MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
                new String[]{
                        MediaStore.Video.Thumbnails.DATA
                },
                MediaStore.Video.Thumbnails.VIDEO_ID + "=" + id,
                null,
                null);
        if (cursor != null && cursor.moveToFirst()) {
            thumbnail = cursor.getString(cursor.getColumnIndex(MediaStore.Video.Media.DATA));
            cursor.close();
        }
        return thumbnail;
    }

    /**
     *if orignal do not Thumbnail, we will create Thumbnail
     */

    public String getVedioThumbnail(Content content, Cursor cursor, String path, String id) {
        String thumbnailPath = getVideoSystemThumbnail(content, id);
        Log.d(TAG, "getVedioThumbnail thumbnailPath is:" + thumbnailPath);
        if (thumbnailPath == null) {
            //create Thumbnail
            MediaStore.Images.Thumbnails.getThumbnail(content.getContentResolver(),
                    cursor.getInt(cursor.getColumnIndex(MediaStore.Video.Media._ID)),
                    MediaStore.Video.Thumbnails.MICRO_KIND, null);
            thumbnailPath = getVideoSystemThumbnail(content, id);
            return thumbnailPath == null ? path : thumbnailPath;
        } else {
            return thumbnailPath;
        }
    }


    public void showAllViedio(Content content) {
		Uri targetUrl = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
        Cursor vidioCursor = content.getContentResolver().query(targetUrl, new String[]{
                        MediaStore.Video.Media.DATA, MediaStore.Video.Media._ID}, null, null,
                MediaStore.Video.Media.DATE_MODIFIED + " DESC");
        String filePath = "";
        String fileType = "";
        string ThumbnailPath = ""; 
        int fileSize;
        if (vidioCursor != null) {
            while (vidioCursor.moveToNext()) {
                    filePath = vidioCursor.getString(vidioCursor.getColumnIndex(MediaStore.Video.Media.DATA));
                    Log.i(TAG, "filePath is:" + filePath);
                    fileType = fullPath.substring(0, filePath.lastIndexOf("/"));
                    Log.i(TAG, "fileType is:" + fileType);
                    file = new File(fullPath);

                    if (!file.exists()) {
                        continue;
                    }
                    //we can get ThumbnailPath
                    ThumbnailPath = getVedioThumbnail(content, vidioCursor, fullPath, vidioCursor.getString(vidioCursor.getColumnIndex(MediaStore.Video.Media._ID)));
                    Log.i(TAG, "ThumbnailPath is:" + ThumbnailPath);   
                    fileSize = file.length();
                    Log.i(TAG, "fileSize is:" + fileSize); 
            }
        }
    }
扫描二维码关注公众号,回复: 3952491 查看本文章

3  如何获取手机所有图片

把上面的Video改成Images,然后就ok了

猜你喜欢

转载自blog.csdn.net/u011068702/article/details/83628654
今日推荐