Android开发之多媒体文件获取工具类实例【音频,视频,图片等】
程序员文章站
2023-12-13 13:05:22
本文实例讲述了android开发之多媒体文件获取工具类。分享给大家供大家参考,具体如下:
package com.android.ocr.util;
impor...
本文实例讲述了android开发之多媒体文件获取工具类。分享给大家供大家参考,具体如下:
package com.android.ocr.util; import java.io.file; import java.util.arraylist; import java.util.list; import android.content.context; import android.database.cursor; import android.graphics.bitmap; import android.provider.mediastore; /** * 根据mediastore查询信息 * @project app_readcard * @package com.android.ocr.util * @author chenlin * @version 1.0 * @date 2013年6月16日 * @note todo */ public class mediastoreutil { private static final string tag = "mediastoreutil"; /** * 查询音频文件名称 * * @param context * @return */ public static list<string> getaudionames(context context) { list<string> list = new arraylist<string>(); cursor cursor = context.getcontentresolver().query( mediastore.audio.media.external_content_uri, new string[] { mediastore.audio.media._id, mediastore.audio.media.display_name, mediastore.audio.media.title, mediastore.audio.media.duration, mediastore.audio.media.artist, mediastore.audio.media.album, mediastore.audio.media.year, mediastore.audio.media.mime_type, mediastore.audio.media.size, mediastore.audio.media.data }, null, new string[] {}, null); while (cursor.movetonext()) { string filename = cursor.getstring(cursor.getcolumnindex(mediastore.audio.media.display_name)); list.add(filename); } return list; } /** * 查询图片文件名称 * * @param context * @return */ public static list<string> getimagenames(context context) { list<string> list = new arraylist<string>(); cursor cursor = context.getcontentresolver().query( mediastore.images.media.external_content_uri, new string[] { mediastore.images.media._id, mediastore.images.media.display_name, mediastore.images.media.title, mediastore.images.media.mime_type, mediastore.images.media.size, mediastore.images.media.data }, null, new string[] {}, null); while (cursor.movetonext()) { logger.i(tag, "filepath==" + mediastore.images.media.data); string filepath = cursor.getstring(cursor.getcolumnindex(mediastore.images.media.data)); string filename = cursor.getstring(cursor.getcolumnindex(mediastore.images.media.display_name)); list.add(filepath + "/" + filename); } return list; } /** * 查询图片文件 * * @param context * @return */ public static list<file> getimages(context context) { list<file> list = new arraylist<file>(); cursor cursor = context.getcontentresolver().query( mediastore.images.media.external_content_uri, new string[] { mediastore.images.media._id, mediastore.images.media.display_name, mediastore.images.media.title, mediastore.images.media.mime_type, mediastore.images.media.size, mediastore.images.media.data }, null, new string[] {}, null); while (cursor.movetonext()) { string filepath = cursor.getstring(cursor.getcolumnindex(mediastore.images.media.data)); logger.i(tag, "filepath==" + filepath); string filename = cursor.getstring(cursor.getcolumnindex(mediastore.images.media.display_name)); //logger.i(tag, "filename==" + filename); file file = new file(filepath); list.add(file); } return list; } /** * 查询文件 * * @param context * @return */ public static list<file> getallfiles(context context) { list<file> list = new arraylist<file>(); cursor cursor = context.getcontentresolver().query( mediastore.images.media.external_content_uri, new string[] { mediastore.images.media._id, mediastore.images.media.display_name, mediastore.images.media.title, mediastore.images.media.mime_type, mediastore.images.media.size, mediastore.images.media.data }, null, new string[] {}, null); while (cursor.movetonext()) { string filepath = cursor.getstring(cursor.getcolumnindex(mediastore.images.media.data)); logger.i(tag, "filepath==" + filepath); string filename = cursor.getstring(cursor.getcolumnindex(mediastore.images.media.display_name)); //logger.i(tag, "filename==" + filename); file file = new file(filepath); list.add(file); } return list; } /** * 获取所有的缩列图 * * @param context * @return */ public static bitmap[] getbitmaps(context context) { bitmap[] bitmaps; string[] projection = { mediastore.images.media._id, mediastore.images.media.data }; cursor cursor = context.getcontentresolver().query(mediastore.images.media.external_content_uri, projection, null, null, mediastore.images.media._id); int count = cursor.getcount(); int image_column_index = cursor.getcolumnindex(mediastore.images.media._id); bitmaps = new bitmap[count]; for (int i = 0; i < count; i++) { cursor.movetoposition(i); int id = cursor.getint(image_column_index); bitmaps[i] = mediastore.images.thumbnails.getthumbnail(context.getcontentresolver(), id, mediastore.images.thumbnails.micro_kind, null); } return bitmaps; } /** * 查询图片缩列文件名称 * * @param context * @return */ public static list<string> getthumbnames(context context) { list<string> list = new arraylist<string>(); cursor cursor = context.getcontentresolver().query( mediastore.images.thumbnails.external_content_uri, new string[] { mediastore.images.thumbnails._id, mediastore.images.thumbnails.data, mediastore.images.thumbnails.kind, mediastore.images.thumbnails.image_id }, null, new string[] {}, null); while (cursor.movetonext()) { string filename = cursor.getstring(cursor.getcolumnindex(mediastore.images.media.display_name)); list.add(filename); } return list; } /** * 获得所有视频文件 * @param context */ public static arraylist<videoinfo> getvideoinfo(context context){ string[] thumbcolumns = new string[]{ mediastore.video.thumbnails.data, mediastore.video.thumbnails.video_id }; string[] mediacolumns = new string[]{ mediastore.video.media.data, mediastore.video.media._id, mediastore.video.media.title, mediastore.video.media.mime_type }; //首先检索sdcard上所有的video cursor cursor = context.getcontentresolver().query(mediastore.video.media.external_content_uri, mediacolumns, null, null, null); arraylist<videoinfo> videolist = new arraylist<videoinfo>(); if(cursor.movetofirst()){ do{ videoinfo info = new videoinfo(); info.filepath = cursor.getstring(cursor.getcolumnindexorthrow(mediastore.video.media.data)); info.mimetype = cursor.getstring(cursor.getcolumnindexorthrow(mediastore.video.media.mime_type)); info.title = cursor.getstring(cursor.getcolumnindexorthrow(mediastore.video.media.title)); //获取当前video对应的id,然后根据该id获取其thumb int id = cursor.getint(cursor.getcolumnindexorthrow(mediastore.video.media._id)); string selection = mediastore.video.thumbnails.video_id +"=?"; string[] selectionargs = new string[]{ id+"" }; cursor thumbcursor = context.getcontentresolver().query(mediastore.video.thumbnails.external_content_uri, thumbcolumns, selection, selectionargs, null); if(thumbcursor.movetofirst()){ info.thumbpath = cursor.getstring(cursor.getcolumnindexorthrow(mediastore.video.thumbnails.data)); } //然后将其加入到videolist videolist.add(info); }while(cursor.movetonext()); } return videolist; } static class videoinfo{ string filepath; string mimetype; string thumbpath; string title; } }
更多关于android相关内容感兴趣的读者可查看本站专题:《android多媒体操作技巧汇总(音频,视频,录音等)》、《android开发入门与进阶教程》、《android视图view技巧总结》、《android文件操作技巧汇总》、《android资源操作技巧汇总》及《android控件用法总结》
希望本文所述对大家android程序设计有所帮助。