Android media workflow multimedia scan path for Android

Source: Internet
Author: User

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 Android media workflow process
We can know that after the android robot scans the media, it will store the media files in the database, and mediaprovider will provide 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 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 Android media workflow process
"
When you receive
Action_boot_completed, action_media_mounted, action_media_scanner_scan_file
Messages are scanned. Previously, the SD card was scanned. When the SD card was mounted, the android system sent an action_media_mounted message.
Yes, media transcoding starts to scan media files. But how can we notify Android media after the/external directory is modified?
What about ghost scan? One way is to restart, and no one is willing to do so. Another method is to run menu> Dev tools> Media
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. Of course there are still some imperfections. You are welcome to correct them.

Supplement)

Why does the Android system automatically call mediascaner to scan files? Study the source code discovery of mediascannerpolicer:

    @Override    public void onReceive(Context context, Intent intent) {        String action = intent.getAction();        Uri uri = intent.getData();        String externalStoragePath = Environment.getExternalStorageDirectory().getPath();        if (action.equals(Intent.ACTION_BOOT_COMPLETED)) {            // scan internal storage            scan(context, MediaProvider.INTERNAL_VOLUME);        } else {            if (uri.getScheme().equals("file")) {                // handle intents related to external storage                String path = uri.getPath();                if (action.equals(Intent.ACTION_MEDIA_MOUNTED) &&                        externalStoragePath.equals(path)) {                    scan(context, MediaProvider.EXTERNAL_VOLUME);                } else if (action.equals(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE) &&                        path != null && path.startsWith(externalStoragePath + "/")) {                    scanFile(context, path);                }            }        }    }

We found that when receiving the action_boot_completed message, the android system will scan the media files in the memory. When the system receives action_media_mounted, it will scan the extended memory in the system (usually the SD card ).


If the system has an SD card, the system can receive the action_media_mounted message and action_boot_completed message at startup,
In this way, the system will scan the media file of the system when it is started. When the SD card file is modified (such as using a data line to connect to a PC), there is usually an action_media_mounted message.
Reported. In this way, media data can be updated in real time.


However, we do not have a real SD card, but set a directory in the memory to the extended memory. Then we will never receive the action_media_mounted message, so we do not
Media files in external storage are automatically scanned. You may ask: Why do you want to run menu> Dev tools> media?
Scan can scan data in external storage memory? The implementation of this code is in the file development/src/COM/Android/Development
/Mediascanneractivity. Java:

@Override    public void onResume() {        super.onResume();        ........        sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())));        ........    }

It can be seen that an action_media_mounted message is broadcast here, so that mediascannerreceiver will scan the media files in the external storage memory.



Related Article

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.