Media File Management on the Android platform is different from that on the desktop. On the desktop system, media files in different directories are displayed in a tree structure. You need to go to different directories to find files in these directories. On the Android platform, media files under different directories are displayed in a list layer. Users can list all media files (of a certain type) without entering sub-directories.
On Android, in order to realize this mode of media file management, extract the metadata of all managed media files, that is, ID3 (MP3 file containing metadata can refer to the http://en.wikipedia.org/wiki/ID3 ), stored in the database and provided as a content provider for other applications. Each time a user displays a media file, a query operation is performed on the database. The multimedia management module consists of three modules:
Multimedia Database
Mediastore is a multimedia database provided by the Android system. Multimedia Information in android can be extracted from this class. This mediastore includes all the information of the multimedia database, including audio, video, and image. Android encapsulates all the multimedia database interfaces, and all databases do not need to be created by themselves, you can directly call contentresolver to remove the encapsulated interfaces for database operations. The usage of multimedia databases is the same as that of sqlite3.
Data in mediastore is updated through a service in mediaprovider after mediascanner scan. The architecture diagram is as follows:
Mediascanner
In the Android system, the multi-media library scans Disk Files Through mediascanner, processes metadata, and saves it to mediastore through mediaprovider. For the mediascannerr framework:
Figure 1-1 mediascanner Framework process
Mediascanner can be manually controlled. In Android, three events have been customized to trigger mediascanner to scan Disk Files: action_boot_completed, action_media_mounted, and action_media_scanner_scan_file. Here, action_boot_completed is the message sent after the system starts, action_media_mounted is the message triggered by the plug-in event, and action_media_scanner_scan_file is generally after some file operations, A message manually sent by a developer to rescan a multimedia file. The sendbroadcast function is used to send messages. For example, to broadcast an action_media_mounted message:
Sendbroadcast (new intent (intent. action_media_mounted, Uri. parse ("file ://"
+ Environment. getexternalstoragedirectory ())));
It can be seen from the above that a broadcast (transfer the corresponding scan requirements) is sent to trigger a re-scan disk event, so we can guess that the system must have a broadcast receiver (when and where to register ?), After receiving the broadcast message, start mediascannerservice with the corresponding parameters. Mediascannerservice calls a public class mediascanner to handle real work. Mediascannerreceiver maintains two types of scan directories: internal volume (internal volume) pointing to $ (android_root)/media. the other is that the external volume points to $ (external_storage), and the scan position can be modified (generally, the external volume is sdcard by default, modify the inand path name of the driver internally.) the specific location of the system source code is shown in Figure 1-1:
Mediascannerpolicer:/mydroid/packages/providers/mediaprovider/src/COM/Android/providers/Media/mediascannerpolicer. Java
The pipeline event reception inherits the broadcastreceiver class and starts mediascannerservice after receiving the scan message. However, note that the oncreate method of the service is called only once, that is, no matter how many times you call startservice and bindservice, the service is only created once. If BIND is started, the onstart method of service is run directly. If start is started, the onbind method is run directly during bind. If you get the BIND first, you can stop it. Right, that is, stopservice is not good. You can only unbindservice first, and then stopservice, therefore, there is a difference between start and bind. (For more information about the bindle interface, see other documents)
Mediascannerservice:
/Mydroid/packages/providers/mediaprovider/src/COM/Android/providers/Media/mediascannerservice. Java
Use this service to call the specific implementation method of mediascanner.
Mediascanner: (method)
\ Frameworks \ base \ media \ Java \ Android \ media \ mediascanner. Java
Mediaplayer overall process:
Thumbnails thumbnail Overview
Thumbnail storage location:/sdcard/dcim/. thumbnails
Thumbnail (large): One JPEG file
All data is stored in the same binary file. thumbdata + version + "-" + hashcode, And the bytes are 10000 bytes.
Details:
For each image, a large thumbnail and a small thumbnail are generated. The large thumbnail is saved on (external device)/sdcard/dcim /. in the thumbnails/directory, the maximum size of a large thumbnail is 512x384, and the minimum value is 1x1. Small thumbnails are uniformly saved to a random access file (external device)/sdcard/dcim/. thumbnails/+ ". thumbdata" + version + "-" + muri. hashcode ()
Store thumbnails in the database
The final endfile () of a file is scanned by mmediaprovider. insert () or mmediaprovider. Update (). In the mmediaprovider. during insert (), for images_media and video_media types: requestmediathumbnail () ---> mcurrentthumbrequest.exe cute (); During execution, the system first queries the thumbnail database and returns the result after the query, thumbnailutil not found. createvideothumbnail (mpath) or thumbnailutil. createimagethumbnail (MCR, mpath, muri, morigid, images. thumbnails. micro_kind, true/* savemini */));
Create Image thumbnails:
Call thumbnailutil when creating thumbnail. makebitmap () is created. If savemini is true, the thumbnail thumbnailutil is saved. storethumbnail (Cr, origid, thumbdata, bitmap. getwidth (), bitmap. getheight (); When saved, it will query the database through origid, find and return the corresponding Uri, insert the database if not found, return the URI, and finally return bitmap. For creating a video thumbnail: Get a bintmap frame in the video file and save the large thumbnail to the file: bitmap. compress (bitmap. compressformat. JPEG, 75, minioutstream); save small thumbnails to Random Access Files: Data = minioutstream. tobytearray (); minithumbfile. saveminithumbtofile (data, morigid, magic );
During mmediaprovider. Update (), magic is queried for the images_media, images_media_id, video_media, and video_media_id types. If not, requestmediathumbnail () is executed. The process is the same as above.
Mediaprovider is the implementation of the above process. See the code in.
Figure 3 mediaprovider code Architecture
From: http://wenku.baidu.com/view/537b9b8ecc22bcd126ff0c97.html #
Anyone familiar with Android knows that android media platform only scans media files on the SD card. For more information about the scan policy, see Android media workflow process. If the sdks are not provided on our hardware platform, why can't Android Play media files? Of course not. Otherwise, the android system will not become a perfect framework. This article introduces how to modify the scan path of a multimedia file based on my experience.
According to the introduction of Android media workflow process, after Android media transcoding completes media scanning, it stores media files in the database, and mediaprovider provides services for upper-layer applications.
After studying the media workflow code, it is found that the scan path is Android. OS. environment. external_storage_directory. The variable file is located:
Frameworks/base/CORE/Java/Android/OS/environment. Java
By default, Android will search for the/sddisk directory:
Private Static final file external_storage_directory
= Getdirectory ("external_storage", "/sddisk ");
To allow it to search for our custom path, you can modify the definition of this variable and add it to the directory we want to scan/external. The code is modified as follows:
Private Static final file external_storage_directory
= Getdirectory ("external_storage", "/external ");
In this way, Android media files will search for the/external directory to find media files.
Next, we need to ensure that this file exists. We need to modify the init. RC file. Add the following definition:
Mkdir/external 0777 System
In this way, if the/external directory does not exist at startup, a file is created. If it already exists, no action is taken.
In addition, how does one trigger media transcoding? According to the introduction in Android media workflow process, a scan is performed only when the action_boot_completed, action_media_mounted, and action_media_scanner_scan_file messages are received. Previously, the SD card was scanned. When the SD card was mounted, the android system sent an action_media_mounted message, and the media scanner began to scan the media files. But after our/external directory is modified, how can we notify Android media workflow scanning? One way is to restart, and no one is willing to do so. Another method is to run menu-> Dev tools-> media scan, which will scan. Currently, I have not enabled automatic scanning after directory modification. If you have a good idea, please leave a message for me.
Through the above steps, you can store media files in the/external directory of Android and play them back by the music application.
Everyone feels that the article is good, just help the application: http://www.talkphone.cn/Down/Soft/Detail/49172_0.html