JNI instance 1 --- scan mp3 files in the SD card

Source: Internet
Author: User

Recently, JNI programming has been studied. By the way, a small demo is implemented. The native recursive method is used to traverse the mp3 file in the mobile phone SD card directory and output the absolute path of the MP3 file on the JNI layer. In terms of execution efficiency, compared with the Java implementation method, the C code on the native layer is much better. This demo is relatively simple, and the complexity lies in the implementation of C functions. Because I have been engaged in Java development for a long time, many things in C language are forgotten. [Cpp] void Merge (JNIEnv * env, jobject obj, jstring jdirPath) {const char * path = (* env)-> GetStringUTFChars (env, jdirPath, NULL ); LOGE ("begin to call scan_dir () in the JNI, and path = % s \ n", path); scan_dir (path);} void scan_dir (const char * directory) {DIR * dp; struct dirent * entry; struct stat statbuf; if (dp = opendir (directory) = NULL) {perror ("opendir" ); Return;} chdir (directory); // LOGE ("pyb chdir directory = % s \ n", directory); while (entry = readdir (dp ))! = NULL) {stat (entry-> d_name, & statbuf); if (S_ISDIR (statbuf. st_mode) {if (strcmp (entry-> d_name ,".")! = 0) & (strcmp (entry-> d_name ,"..")! = 0) & (entry-> d_name [0]! = '.') {Scan_dir (entry-> d_name) ;}} else {int size = strlen (entry-> d_name); if (entry-> d_name [0]! = '. '& (Statbuf. st_size/1024)> 300 // greater than 300 kb, indicating that mp3 files are allowed (ignore <kb mp3 files) & strcmp (entry-> d_name + (size-4 ), ". mp3 ") = 0) {// LOGE (" scan_dir (), file st_size = % d \ n ", (statbuf. st_size/1024); char * parentPath = (char *) malloc (1024); char * absolutePath = (char *) malloc (1024 ); // first obtain the working path getcwd (parentPath, 1024); // LOGE ("parentPath = % s \ n", parentPath); strcpy (absolutePath, parentPath ); char * p = "/"; AbsolutePath = strcat (absolutePath, p); absolutePath = strcat (absolutePath, entry-> d_name); // statbuf. st_size, LOGE ("scan_dir (), file absolutePath = % s \ n", absolutePath); free (parentPath); parentPath = NULL; free (absolutePath ); absolutePath = NULL ;}} chdir (".. "); closedir (dp);} Although this demo is more efficient than native and Java, native functions in the Code are still less efficient. Some music players on the market, such as cool dog and domy music, have the function of scanning SD card audio files in their app, and the execution speed is much faster than in my demo, the efficiency is about 5 times in my demo.

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.