SoFunction
Updated on 2025-03-11

Android programming method to obtain video, audio and pictures of multimedia library

This article describes the method of Android programming to obtain video, audio and pictures of multimedia libraries. Share it for your reference, as follows:

Find relevant information about audio, video, and picture files from the media library, and obtain thumbnails of video, pictures, and album pictures
and some file operations

package ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
import ;
public class FileOper {
public static final String [] IMAGE_COLUMN = {,};
public static final String [] AUDIO_COLUMN = {,,,
._ID,.ALBUM_ID};
public static final String [] VIDEO_COLUMN = {,,};
private static final Uri sArtworkUri = ("content://media/external/audio/albumart");
///Create a folderpublic static void createFile(String path){
File file = new File(path);
if(!()){
();
}
}
//Delete the file and refresh the media librarypublic static void deleteFile(String path,final Context context,final int type){
File file = new File(path);
if(()){
if(()){
();
}else if(()){
deleteDirs(file);
}
}
/// Listen to the refresh of media library(context, new String[]{"/storage/sdcard0"}, null,new OnScanCompletedListener(){
@Override
public void onScanCompleted(String path, Uri uri) {//// After refreshing, do the corresponding processingif(type == 1){
String lists = (context);
(context, SentMessage.WATCH_PATH, lists);
}else if(type == 2){
String lists = (context);
(context, SentMessage.WATCH_PATH, lists);
}else if(type == 3){
String lists = (context);
(context, SentMessage.WATCH_PATH, lists);
}
}
});/////
}
public static void deleteDirs(File file){
if(()){
File[]files = ();
if(files == null ||  == 0){
();
}else {
for(int i = 0;i<;i++){
if(files[i].isDirectory()){
deleteDirs(files[i]);
}else{
files[i].delete();
}
}
}
();
}
}
///Get the files and folders in the corresponding directorypublic static String getFileInfoList(String path) {
File file = new File(path);
String lists = "";
File[] files = ();
if(files != null){
if(>0){
for(int i = 0;i<;i++){
if(()){
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;
}
/////Query video filespublic static String getVideoFile(Context context){
ContentResolver resolver = ();
Cursor cursor = (.EXTERNAL_CONTENT_URI, VIDEO_COLUMN, null, null, null);
String tp = "video}";
if(cursor != null){
if(()){
String path = ((VIDEO_COLUMN[0]));
String size = ((VIDEO_COLUMN[1]));
String duration = ((VIDEO_COLUMN[2]));
//String thumbnail = (getVideoThumbnail(path, 96, 96, .MICRO_KIND));
//tp = tp+Util.encodeBase64(path)+","+size+","+duration+","+thumbnail+"}";
tp = tp+Util.encodeBase64(path)+","+size+","+duration+"}";
while(()){
path = ((VIDEO_COLUMN[0]));
size = ((VIDEO_COLUMN[1]));
duration = ((VIDEO_COLUMN[2]));
//tp = tp+Util.encodeBase64(path)+","+size+","+duration+","+thumbnail+"}";
tp = tp+Util.encodeBase64(path)+","+size+","+duration+"}";
}
}
}
return tp;
}
//Query audio filespublic static String getAudioFiles(Context context){
ContentResolver resolver = ();
Cursor cursor = (.EXTERNAL_CONTENT_URI, AUDIO_COLUMN, null, null, null);
String tp = "audio}";
if(cursor != null){
if(()){
String path = ((AUDIO_COLUMN[0]));
String size = ((AUDIO_COLUMN[1]));
String duration = ((AUDIO_COLUMN[2]));
/*String id = ((AUDIO_COLUMN[3]));
String albumid = ((AUDIO_COLUMN[4]));
Bitmap bt = getMusicBitemp(context,(id),(albumid));
String thumbnail = "audio";
if(bt != null){
thumbnail = (getAudioThumbnail(bt, 96, 96));
}
tp = tp+Util.encodeBase64(path)+","+size+","+duration+","+thumbnail+"}";*/
tp = tp+Util.encodeBase64(path)+","+size+","+duration+"}";
while(()){
path = ((AUDIO_COLUMN[0]));
size = ((AUDIO_COLUMN[1]));
duration = ((AUDIO_COLUMN[2]));
/*id = ((AUDIO_COLUMN[3]));
albumid = ((AUDIO_COLUMN[4]));
bt = getMusicBitemp(context,(id),(albumid));
thumbnail = "audio";
if(bt != null){
thumbnail = (getAudioThumbnail(bt, 96, 96));
}
tp = tp+Util.encodeBase64(path)+","+size+","+duration+","+thumbnail+"}";*/
tp = tp+Util.encodeBase64(path)+","+size+","+duration+"}";
}
}
}
return tp;
}
/////Query picturespublic static String getImageFiles(Context context){
ContentResolver resolver = ();
Cursor cursor = (.EXTERNAL_CONTENT_URI, IMAGE_COLUMN, null, null, null);
String tp = "image}";
if(cursor != null){
if(()){
String path = ((IMAGE_COLUMN[0]));
String size = ((IMAGE_COLUMN[1]));
/*String thumbnail = (getImageThumbnail(path, 96, 96));
tp = tp+Util.encodeBase64(path)+","+size+","+thumbnail+"}";*/
tp = tp+Util.encodeBase64(path)+","+size+"}";
while(()){
path = ((AUDIO_COLUMN[0]));
size = ((AUDIO_COLUMN[1]));
/*thumbnail = (getImageThumbnail(path, 96, 96));
tp = tp+Util.encodeBase64(path)+","+size+","+thumbnail+"}";*/
tp = tp+Util.encodeBase64(path)+","+size+"}";
}
}
}
return tp;
}
///Get audio file album picturespublic 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 = ("content://media/external/audio/media/"
+ songid + "/albumart");
ParcelFileDescriptor pfd = ()
.openFileDescriptor(uri, "r");
if (pfd != null) {
FileDescriptor fd = ();
bm = (fd);
}
} else {
Uri uri = (sArtworkUri, albumid);
ParcelFileDescriptor pfd = ()
.openFileDescriptor(uri, "r");
if (pfd != null) {
FileDescriptor fd = ();
bm = (fd);
} else {
return null;
}
}
} catch (FileNotFoundException ex) {
return null;
}
return bm;
}
//// Get picture thumbnailpublic static Bitmap getImageThumbnail(String imagePath, int width, int height) { 
Bitmap bitmap = null; 
 options = new (); 
 = true; 
bitmap = (imagePath, options); 
 = false; 
int h = ; 
int w = ; 
int beWidth = w / width; 
int beHeight = h / height; 
int be = 1; 
if (beWidth < beHeight) { 
be = beWidth; 
} else { 
be = beHeight; 
} 
if (be <= 0) { 
be = 1; 
} 
 = be; 
bitmap = (imagePath, options); 
bitmap = (bitmap, width, height, 
ThumbnailUtils.OPTIONS_RECYCLE_INPUT); 
return bitmap; 
} 
//// Get thumbnails of audio album picturespublic static Bitmap getAudioThumbnail(Bitmap bitmap, int width, int height) { 
 options = new (); 
int h = (); 
int w = (); 
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 = (bitmap, width, height, 
ThumbnailUtils.OPTIONS_RECYCLE_INPUT); 
return bitmap; 
} 
///// Get video file thumbnailpublic static Bitmap getVideoThumbnail(String videoPath, int width, int height, 
int kind) { 
Bitmap bitmap = null; 
bitmap = (videoPath, kind); 
("w"+()); 
("h"+()); 
bitmap = (bitmap, width, height, 
ThumbnailUtils.OPTIONS_RECYCLE_INPUT); 
return bitmap; 
} 
///Get file sizepublic static int getFileSize(File file){
InputStream is=null;
int size=0;
try {
is=new FileInputStream(file);
size= ();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
();
}catch (IOException e) {
// TODO Auto-generated catch block
();
}finally{
if(is!=null){
try {
();
} catch (IOException e) {
// TODO Auto-generated catch block
();
}
}
}
return size;
}
}

For more information about Android related content, please check out the topic of this site:Android development introduction and advanced tutorial》、《Android multimedia operation skills summary (audio, video, recording, etc.)》、《Android View View Tips Summary》、《Android programming activity operation skills summary》、《Summary of Android data skills for operating json format》、《Android resource operation skills summary"and"Android control usage summary

I hope this article will be helpful to everyone's Android programming design.