Android obtains videos, audios, and images from multi-media libraries,

Source: Internet
Author: User

Android obtains videos, audios, and images from multi-media libraries,

Search for audio, video, and image file information from the media library, and obtain thumbnails of videos, images, and album images.

And some file operations

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 filepath {
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 ");

/// Create a folder

Public static void createFile (String path ){
File file = new File (path );
If (! File. exists ()){
File. mkdirs ();
}
}

// Delete the file and refresh the media library
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 );
}

}

/// Listen to the media repository refresh status

MediaScannerConnection. scanFile (context, new String [] {"/storage/sdcard0"}, null, new OnScanCompletedListener (){


@ Override
Public void onScanCompleted (String path, Uri uri) {
If (type = 1 ){
String lists = filetings. getVideoFile (context );
SentMessage. SendToBluetooth (context, SentMessage. WATCH_PATH, lists );
} Else if (type = 2 ){
String lists = filetings. getAudioFiles (context );
SentMessage. SendToBluetooth (context, SentMessage. WATCH_PATH, lists );
} Else if (type = 3 ){
String lists = filetings. 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 ();
}
}

/// Obtain the files and folders in the corresponding directory

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;
}

//// Query the video file
Public static String getVideoFile (Context context ){
ContentResolver resolver = context. getContentResolver ();
Cursor cursor = resolver. query (MediaStore. Video. Media. EXTERNAL_CONTENT_URI, VIDEO_COLUMN, 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;
}

// Query audio files
Public static String getAudioFiles (Context context ){
ContentResolver resolver = context. getContentResolver ();
Cursor cursor = resolver. query (MediaStore. Audio. Media. EXTERNAL_CONTENT_URI, AUDIO_COLUMN, 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;
}

//// Query the image
Public static String getImageFiles (Context context ){
ContentResolver resolver = context. getContentResolver ();
Cursor cursor = resolver. query (MediaStore. Images. Media. EXTERNAL_CONTENT_URI, IMAGE_COLUMN, 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;
}

/// Obtain the album image of the audio file
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;
}

//// Obtain the image thumbnail
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;
}


//// Obtain the thumbnail of the audio album Image
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;
}
///// Obtain the video file thumbnail
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;
}

/// Get the file size
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;
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.