Modify the scan path of Android Media Transcoding

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
Media workflow process, we can know that after Android idea scans media, 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 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
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. 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 there is an SD card in the system, the system can receive the ACTION_MEDIA_MOUNTED message and ACTION_BOOT_COMPLETED message when starting the system. In this way, the system will scan media files when starting the system. When the SD card file is modified (for example, the data line is used to connect to the PC), The ACTION_MEDIA_MOUNTED message is usually 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. Therefore, we will never receive ACTION_MEDIA_MOUNTED messages, so we will not automatically scan media files in external storage. Then you may ask: why can I run menu-> dev tools-> Media Scan to Scan data in the 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.


From: Walking in the cloudHttp://my.unix-center.net /~ Simon_fu /? P = 549

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.