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

时间:2021-05-20

如何判断文件呢? 可以通过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;
}

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章