本文实例讲述了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程序设计有所帮助。
多媒体 HTML5 前的多媒体需要借助第三方插件,例如 Flash,但是 HTML5 将网页中的多媒体带入了新的一章。 基本用法 // 音频 // 指定资源类型可以帮助浏览器更快的定位解码 <audio autobuffer autoloop loop controls> <source src="/media/audio.mp3" type="audio/mpeg"> <source s
我正在制作一个音乐播放器应用程序,它将从本地存储中获取歌曲
本文向大家介绍android提取视频多张图片和视频信息实例,包括了android提取视频多张图片和视频信息实例的使用技巧和注意事项,需要的朋友参考一下 话说2016年的直播比较火,2017年短视频又火了。但对于开发者来说隐藏在这背后的技术才是我们所关心的,毕竟我们是靠技术吃饭的。 现在在安卓中多媒体服务比较强大,而与视频有关的视频基本处理技术有必要学习一下。我前段时间也在做有关视频的一些需求,当然
本文向大家介绍Python中操作各种多媒体,视频、音频到图片的代码详解,包括了Python中操作各种多媒体,视频、音频到图片的代码详解的使用技巧和注意事项,需要的朋友参考一下 我们经常会遇到一些对于多媒体文件修改的操作,像是对视频文件的操作:视频剪辑、字幕编辑、分离音频、视频音频混流等。又比如对音频文件的操作:音频剪辑,音频格式转换。再比如我们最常用的图片文件,格式转换、各个属性的编辑等。因为多媒
大家好,我正在尝试使用两个独立的媒体播放器实例播放音频和视频文件。当我从一开始播放它时,它工作得很好。但当我寻找时,我可以看到音频和视频的延迟 这是我寻找音频和视频的代码 //sikAudio(msec); if(media播放器!=null) { // 没有直播流,如果(medialayer.get持续时间() }
本文向大家介绍Android中简单调用图片、视频、音频、录音和拍照的方法,包括了Android中简单调用图片、视频、音频、录音和拍照的方法的使用技巧和注意事项,需要的朋友参考一下 本文实例讲述了Android中简单调用图片、视频、音频、录音和拍照的方法。分享给大家供大家参考,具体如下: 更多关于Android相关内容感兴趣的读者可查看本站专题:《Android多媒体操作技巧汇总(音频,视频,录音等