Android開(kāi)發(fā)之多媒體文件獲取工具類實(shí)例【音頻,視頻,圖片等】
本文實(shí)例講述了Android開(kāi)發(fā)之多媒體文件獲取工具類。分享給大家供大家參考,具體如下:
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;
/**
* 根據(jù)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));
//獲取當(dāng)前Video對(duì)應(yīng)的Id,然后根據(jù)該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;
}
}
更多關(guān)于Android相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android開(kāi)發(fā)入門與進(jìn)階教程》、《Android視圖View技巧總結(jié)》、《Android文件操作技巧匯總》、《Android資源操作技巧匯總》及《Android控件用法總結(jié)》
希望本文所述對(duì)大家Android程序設(shè)計(jì)有所幫助。
- Android中使用Bitmap類將矩形圖片轉(zhuǎn)為圓形的方法
- Android圖片加載的緩存類
- 非常實(shí)用的Android圖片工具類
- Android開(kāi)發(fā)之圖片壓縮工具類完整實(shí)例
- Android開(kāi)發(fā)實(shí)現(xiàn)的IntentUtil跳轉(zhuǎn)多功能工具類【包含視頻、音頻、圖片、攝像頭等操作功能】
- Android開(kāi)發(fā)之超強(qiáng)圖片工具類BitmapUtil完整實(shí)例
- Android圖片處理工具類BitmapUtils
- Android開(kāi)發(fā)之圖片切割工具類定義與用法示例
- Android編程圖片加載類ImageLoader定義與用法實(shí)例分析
- Android編程圖片操作類定義與用法示例【拍照,相冊(cè)選圖及裁剪】
相關(guān)文章
Android 動(dòng)態(tài)菜單實(shí)現(xiàn)實(shí)例代碼
這篇文章主要介紹了Android 動(dòng)態(tài)菜單實(shí)現(xiàn)實(shí)例代碼的相關(guān)資料,這里附有實(shí)例代碼及實(shí)現(xiàn)效果圖,需要的朋友可以參考下2017-01-01
Android中SurfaceView和view畫出觸摸軌跡
這篇文章主要介紹了Android中SurfaceView和view畫出觸摸軌跡的相關(guān)資料,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-03-03
Android學(xué)習(xí)筆記-保存文件(Saving Files)
這篇文章主要介紹了Android中保存文件(Saving Files)的方法,需要的朋友可以參考下2014-10-10
Android實(shí)現(xiàn)圖片瀏覽并改變透明度
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)圖片瀏覽并改變透明度,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-08-08
Flutter實(shí)現(xiàn)Android滾動(dòng)懸浮效果過(guò)程
這篇文章主要介紹了Flutter實(shí)現(xiàn)Android滾動(dòng)懸浮效果,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-01-01
Android給TextView添加點(diǎn)擊事件的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇Android給TextView添加點(diǎn)擊事件的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12
解決android6.0以上不能讀取外部存儲(chǔ)權(quán)限的問(wèn)題
今天小編就為大家分享一篇解決android6.0以上不能讀取外部存儲(chǔ)權(quán)限的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08
Android Camera2采集攝像頭原始數(shù)據(jù)
這篇文章主要介紹了Android Camera2采集攝像頭原始數(shù)據(jù)并進(jìn)行手工預(yù)覽的功能實(shí)現(xiàn)原理以及代碼分析,需要的朋友學(xué)習(xí)下吧。2018-02-02
基于Android自定義控件實(shí)現(xiàn)刮刮樂(lè)效果
這篇文章主要介紹了基于Android自定義控件實(shí)現(xiàn)刮刮樂(lè)效果 的相關(guān)資料,需要的朋友可以參考下2015-12-12

