亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android編程實現獲取多媒體庫視頻、音頻、圖片的方法

發布時間:2020-09-19 12:48:03 來源:腳本之家 閱讀:211 作者:釋汐宇辰 欄目:移動開發

本文實例講述了Android編程實現獲取多媒體庫視頻、音頻、圖片的方法。分享給大家供大家參考,具體如下:

從媒體庫中查找音頻、視頻、圖片文件的相關信息,并獲取視頻、圖片、專輯圖片的縮略圖
和一些文件操作

package com.uwatch.swconnectservice.util;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.media.MediaScannerConnection;
import android.media.MediaScannerConnection.OnScanCompletedListener;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.ParcelFileDescriptor;
import android.provider.MediaStore;
import android.util.Log;
public class FileOper {
public static final String [] IMAGE_COLUMN = {MediaStore.Images.Media.DATA,MediaStore.Images.Media.SIZE};
public static final String [] AUDIO_COLUMN = {MediaStore.Audio.Media.DATA,MediaStore.Audio.Media.SIZE,MediaStore.Audio.Media.DURATION,
MediaStore.Audio.Media._ID,MediaStore.Audio.Media.ALBUM_ID};
public static final String [] VIDEO_COLUMN = {MediaStore.Video.Media.DATA,MediaStore.Video.Media.SIZE,MediaStore.Audio.Media.DURATION};
private static final Uri sArtworkUri = Uri.parse("content://media/external/audio/albumart");
///創建文件夾
public static void createFile(String path){
File file = new File(path);
if(!file.exists()){
file.mkdirs();
}
}
//刪除文件,并刷新媒體庫
public static void deleteFile(String path,final Context context,final int type){
File file = new File(path);
if(file.exists()){
if(file.isFile()){
file.delete();
}else if(file.isDirectory()){
deleteDirs(file);
}
}
///監聽媒體庫刷新情況
MediaScannerConnection.scanFile(context, new String[]{"/storage/sdcard0"}, null,new OnScanCompletedListener(){
@Override
public void onScanCompleted(String path, Uri uri) {////刷新完后做相應處理
if(type == 1){
String lists = FileOper.getVideoFile(context);
SentMessage.SendToBluetooth(context, SentMessage.WATCH_PATH, lists);
}else if(type == 2){
String lists = FileOper.getAudioFiles(context);
SentMessage.SendToBluetooth(context, SentMessage.WATCH_PATH, lists);
}else if(type == 3){
String lists = FileOper.getImageFiles(context);
SentMessage.SendToBluetooth(context, SentMessage.WATCH_PATH, lists);
}
}
});/////
}
public static void deleteDirs(File file){
if(file.isDirectory()){
File[]files = file.listFiles();
if(files == null || files.length == 0){
file.delete();
}else {
for(int i = 0;i<files.length;i++){
if(files[i].isDirectory()){
deleteDirs(files[i]);
}else{
files[i].delete();
}
}
}
file.delete();
}
}
///獲取相應目錄下的文件、文件夾
public static String getFileInfoList(String path) {
File file = new File(path);
String lists = "";
File[] files = file.listFiles();
if(files != null){
if(files.length>0){
for(int i = 0;i<files.length;i++){
if(file.exists()){
if(files[i].isFile()){
lists = lists+"1,"+Util.encodeBase64(files[i].getName())+","+Util.encodeBase64(files[i].getAbsolutePath())+","+getFileSize(files[i])+"}";
}else if(files[i].isDirectory()){
lists = lists+"0,"+Util.encodeBase64(files[i].getName())+","+Util.encodeBase64(files[i].getAbsolutePath())+",0}";
} 
}
}
}
}
return lists;
}
////查詢視頻文件
public static String getVideoFile(Context context){
ContentResolver resolver = context.getContentResolver();
Cursor cursor = resolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, VIDEO_COLUMN, null, null, null);
String tp = "video}";
if(cursor != null){
if(cursor.moveToFirst()){
String path = cursor.getString(cursor.getColumnIndex(VIDEO_COLUMN[0]));
String size = cursor.getString(cursor.getColumnIndex(VIDEO_COLUMN[1]));
String duration = cursor.getString(cursor.getColumnIndex(VIDEO_COLUMN[2]));
//String thumbnail = Util.bitmaptoString(getVideoThumbnail(path, 96, 96, MediaStore.Images.Thumbnails.MICRO_KIND));
//tp = tp+Util.encodeBase64(path)+","+size+","+duration+","+thumbnail+"}";
tp = tp+Util.encodeBase64(path)+","+size+","+duration+"}";
while(cursor.moveToNext()){
path = cursor.getString(cursor.getColumnIndex(VIDEO_COLUMN[0]));
size = cursor.getString(cursor.getColumnIndex(VIDEO_COLUMN[1]));
duration = cursor.getString(cursor.getColumnIndex(VIDEO_COLUMN[2]));
//tp = tp+Util.encodeBase64(path)+","+size+","+duration+","+thumbnail+"}";
tp = tp+Util.encodeBase64(path)+","+size+","+duration+"}";
}
}
}
return tp;
}
//查詢音頻文件
public static String getAudioFiles(Context context){
ContentResolver resolver = context.getContentResolver();
Cursor cursor = resolver.query(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, AUDIO_COLUMN, null, null, null);
String tp = "audio}";
if(cursor != null){
if(cursor.moveToFirst()){
String path = cursor.getString(cursor.getColumnIndex(AUDIO_COLUMN[0]));
String size = cursor.getString(cursor.getColumnIndex(AUDIO_COLUMN[1]));
String duration = cursor.getString(cursor.getColumnIndex(AUDIO_COLUMN[2]));
/*String id = cursor.getString(cursor.getColumnIndex(AUDIO_COLUMN[3]));
String albumid = cursor.getString(cursor.getColumnIndex(AUDIO_COLUMN[4]));
Bitmap bt = getMusicBitemp(context,Long.valueOf(id),Long.valueOf(albumid));
String thumbnail = "audio";
if(bt != null){
thumbnail = Util.bitmaptoString(getAudioThumbnail(bt, 96, 96));
}
tp = tp+Util.encodeBase64(path)+","+size+","+duration+","+thumbnail+"}";*/
tp = tp+Util.encodeBase64(path)+","+size+","+duration+"}";
while(cursor.moveToNext()){
path = cursor.getString(cursor.getColumnIndex(AUDIO_COLUMN[0]));
size = cursor.getString(cursor.getColumnIndex(AUDIO_COLUMN[1]));
duration = cursor.getString(cursor.getColumnIndex(AUDIO_COLUMN[2]));
/*id = cursor.getString(cursor.getColumnIndex(AUDIO_COLUMN[3]));
albumid = cursor.getString(cursor.getColumnIndex(AUDIO_COLUMN[4]));
bt = getMusicBitemp(context,Long.valueOf(id),Long.valueOf(albumid));
thumbnail = "audio";
if(bt != null){
thumbnail = Util.bitmaptoString(getAudioThumbnail(bt, 96, 96));
}
tp = tp+Util.encodeBase64(path)+","+size+","+duration+","+thumbnail+"}";*/
tp = tp+Util.encodeBase64(path)+","+size+","+duration+"}";
}
}
}
return tp;
}
////查詢圖片
public static String getImageFiles(Context context){
ContentResolver resolver = context.getContentResolver();
Cursor cursor = resolver.query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_COLUMN, null, null, null);
String tp = "image}";
if(cursor != null){
if(cursor.moveToFirst()){
String path = cursor.getString(cursor.getColumnIndex(IMAGE_COLUMN[0]));
String size = cursor.getString(cursor.getColumnIndex(IMAGE_COLUMN[1]));
/*String thumbnail = Util.bitmaptoString(getImageThumbnail(path, 96, 96));
tp = tp+Util.encodeBase64(path)+","+size+","+thumbnail+"}";*/
tp = tp+Util.encodeBase64(path)+","+size+"}";
while(cursor.moveToNext()){
path = cursor.getString(cursor.getColumnIndex(AUDIO_COLUMN[0]));
size = cursor.getString(cursor.getColumnIndex(AUDIO_COLUMN[1]));
/*thumbnail = Util.bitmaptoString(getImageThumbnail(path, 96, 96));
tp = tp+Util.encodeBase64(path)+","+size+","+thumbnail+"}";*/
tp = tp+Util.encodeBase64(path)+","+size+"}";
}
}
}
return tp;
}
///獲取音頻文件專輯圖片
public static Bitmap getMusicBitemp(Context context, Long songid,
Long albumid) {
Bitmap bm = null;
if (albumid < 0 && songid < 0) {
throw new IllegalArgumentException(
"Must specify an album or a song id");
}
try {
if (albumid < 0) {
Uri uri = Uri.parse("content://media/external/audio/media/"
+ songid + "/albumart");
ParcelFileDescriptor pfd = context.getContentResolver()
.openFileDescriptor(uri, "r");
if (pfd != null) {
FileDescriptor fd = pfd.getFileDescriptor();
bm = BitmapFactory.decodeFileDescriptor(fd);
}
} else {
Uri uri = ContentUris.withAppendedId(sArtworkUri, albumid);
ParcelFileDescriptor pfd = context.getContentResolver()
.openFileDescriptor(uri, "r");
if (pfd != null) {
FileDescriptor fd = pfd.getFileDescriptor();
bm = BitmapFactory.decodeFileDescriptor(fd);
} else {
return null;
}
}
} catch (FileNotFoundException ex) {
return null;
}
return bm;
}
////獲取圖片縮略圖
public static Bitmap getImageThumbnail(String imagePath, int width, int height) { 
Bitmap bitmap = null; 
BitmapFactory.Options options = new BitmapFactory.Options(); 
options.inJustDecodeBounds = true; 
bitmap = BitmapFactory.decodeFile(imagePath, options); 
options.inJustDecodeBounds = false; 
int h = options.outHeight; 
int w = options.outWidth; 
int beWidth = w / width; 
int beHeight = h / height; 
int be = 1; 
if (beWidth < beHeight) { 
be = beWidth; 
} else { 
be = beHeight; 
} 
if (be <= 0) { 
be = 1; 
} 
options.inSampleSize = be; 
bitmap = BitmapFactory.decodeFile(imagePath, options); 
bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height, 
ThumbnailUtils.OPTIONS_RECYCLE_INPUT); 
return bitmap; 
} 
////獲取音頻專輯圖片的縮略圖
public static Bitmap getAudioThumbnail(Bitmap bitmap, int width, int height) { 
BitmapFactory.Options options = new BitmapFactory.Options(); 
int h = bitmap.getHeight(); 
int w = bitmap.getWidth(); 
int beWidth = w / width; 
int beHeight = h / height; 
int be = 1; 
if (beWidth < beHeight) { 
be = beWidth; 
} else { 
be = beHeight; 
} 
if (be <= 0) { 
be = 1; 
} 
bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height, 
ThumbnailUtils.OPTIONS_RECYCLE_INPUT); 
return bitmap; 
} 
/////獲取視頻文件縮略圖
public static Bitmap getVideoThumbnail(String videoPath, int width, int height, 
int kind) { 
Bitmap bitmap = null; 
bitmap = ThumbnailUtils.createVideoThumbnail(videoPath, kind); 
System.out.println("w"+bitmap.getWidth()); 
System.out.println("h"+bitmap.getHeight()); 
bitmap = ThumbnailUtils.extractThumbnail(bitmap, width, height, 
ThumbnailUtils.OPTIONS_RECYCLE_INPUT); 
return bitmap; 
} 
///獲取文件大小
public static int getFileSize(File file){
InputStream is=null;
int size=0;
try {
is=new FileInputStream(file);
size= is.available();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(is!=null){
try {
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
return size;
}
}

更多關于Android相關內容感興趣的讀者可查看本站專題:《Android開發入門與進階教程》、《Android多媒體操作技巧匯總(音頻,視頻,錄音等)》、《Android視圖View技巧總結》、《Android編程之activity操作技巧總結》、《Android操作json格式數據技巧總結》、《Android資源操作技巧匯總》及《Android控件用法總結》

希望本文所述對大家Android程序設計有所幫助。

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

乐亭县| 正镶白旗| 电白县| 阳高县| 东乡族自治县| 昌图县| 海安县| 五峰| 安顺市| 延寿县| 陇南市| 泗水县| 涿鹿县| 通城县| 射阳县| 陆河县| 城市| 鄂托克旗| 秭归县| 玉溪市| 镇远县| 保定市| 浑源县| 阳高县| 文安县| 临泽县| 平果县| 佛山市| 文登市| 明星| 望谟县| 淮阳县| 萝北县| 漾濞| 凤凰县| 武邑县| 贺州市| 望都县| 武汉市| 延川县| 长春市|