欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android获取SD卡上图片和视频缩略图的小例子

程序员文章站 2023-12-03 14:24:16
      如何判断文件呢? 可以通过cursor遍历数据库,对比internal_content_uri字段的值,这是一...

      如何判断文件呢? 可以通过cursor遍历数据库,对比internal_content_uri字段的值,这是一个uri,这里保存着android手机sd卡上的多媒体文件完整路径。

[java]

复制代码 代码如下:

uri originaluri = mediastore.images.media.external_content_uri;
                //若为视频则为mediastore.video.media.external_content_uri; 
                contentresolver cr = this.getcontentresolver();
                cursor cursor = cr.query(originaluri, null, null, null, null);
                if (cursor == null) {
                    return;
                }
                for (cursor.movetofirst();!cursor.isafterlast(); cursor.movetonext()) {
                    long thumbnailsid = cursor.getlong(cursor   .getcolumnindex("_id"));
                    bitmap bitmap = mediastore.images.thumbnails.getthumbnail(cr,
                            thumbnailsid, video.thumbnails.micro_kind, null);
                    //若为视频则为 
//                  bitmap bitmap = mediastore.video.thumbnails.getthumbnail(cr, 
//                          thumbnailsid, video.thumbnails.micro_kind, null); 
                    return bitmap;
                }

uri originaluri = mediastore.images.media.external_content_uri;
    //若为视频则为mediastore.video.media.external_content_uri;
    contentresolver cr = this.getcontentresolver();
    cursor cursor = cr.query(originaluri, null, null, null, null);
    if (cursor == null) {
     return;
    }
    for (cursor.movetofirst();!cursor.isafterlast(); cursor.movetonext()) {
     long thumbnailsid = cursor.getlong(cursor .getcolumnindex("_id"));
     bitmap bitmap = mediastore.images.thumbnails.getthumbnail(cr,
       thumbnailsid, video.thumbnails.micro_kind, null);
     //若为视频则为
//     bitmap bitmap = mediastore.video.thumbnails.getthumbnail(cr,
//       thumbnailsid, video.thumbnails.micro_kind, null);
     return bitmap;
    }