您好,登錄后才能下訂單哦!
本文實例講述了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程序設計有所幫助。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。