Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android MediaScannerJNI源碼詳解

Android MediaScannerJNI源碼詳解

編輯:關於Android編程

1.簡介

MediaScannerJNI的在MediaScanner中的地位可參考
Android MediaScanner 總綱
MediaScanner JNI文件名:
android_media_MediaScanner.cpp
位置:
frameworksasemediajniandroid_media_MediaScanner.cpp
android_media_MediaScanner是JAVA層和server層溝通的一個橋梁
這裡寫圖片描述
android_media_MediaScanner中有一個MyMediaScannerClient是一個調用JAVA層android/media/MediaScannerClient的入口是Native層想java層通信的橋梁。<喎?/kf/ware/vc/" target="_blank" class="keylink">vcD4NCjxzdmcgaGVpZ2h0PQ=="260" version="1.1" width="634" xmlns="http://www.w3.org/2000/svg">Created with Raphaël 2.1.0(java)MediaScanner(java)MediaScanner(native)android_media_MediaScanner(native)android_media_MediaScanner(C++)MediaScanner(C++)MediaScannermedia_jniMyMediaScannerClient

2. 初始化

在MediaScanner.java初始化中過程調用,其核心是創建了一個StagefrightMediaScanner

static {
        System.loadLibrary(media_jni);
        native_init();
    }
    public MediaScanner(Context c) {
        native_setup();
        ......
    }

native_init 對應 android_media_MediaScanner_native_init
native_setup對應android_media_MediaScanner_native_setup

static void
android_media_MediaScanner_native_setup(JNIEnv *env, jobject thiz)
{
    ALOGV(native_setup);
    MediaScanner *mp = new StagefrightMediaScanner;
    ......
    env->SetLongField(thiz, fields.context, (jlong)mp);
}

3. android_media_MediaScanner_processDirectory

調用MediaScanner.cpp實現processDirectory完成工作
在MediaScanner.java中調用scanDirectories,詳見Android MediaScanner源代碼解析 第6節
其功能還是通過MediaScanner.cpp的processDirectory實現對

static void
android_media_MediaScanner_processDirectory(
        JNIEnv *env, jobject thiz, jstring path, jobject client)
{
    MediaScanner *mp = getNativeScanner_l(env, thiz);
    .......
    const char *pathStr = env->GetStringUTFChars(path, NULL);
    MyMediaScannerClient myClient(env, client);
    MediaScanResult result = mp->processDirectory(pathStr, myClient);
    env->ReleaseStringUTFChars(path, pathStr);
}

4. android_media_MediaScanner_processFile

調用StageFrightMediaScanner.cpp實現processFile完成工作

static void
android_media_MediaScanner_processFile(
        JNIEnv *env, jobject thiz, jstring path,
        jstring mimeType, jobject client)
{
    // Lock already hold by processDirectory
    MediaScanner *mp = getNativeScanner_l(env, thiz);
    const char *pathStr = env->GetStringUTFChars(path, NULL);

    const char *mimeTypeStr =
        (mimeType ? env->GetStringUTFChars(mimeType, NULL) : NULL);
    ........
    MyMediaScannerClient myClient(env, client);
    MediaScanResult result = mp->processFile(pathStr, mimeTypeStr, myClient);
}

5. MyMediaScannerClient

引用android/media/MediaScannerClient中scanFile,handleStringTag, setMimeType三個接口

        jclass mediaScannerClientInterface =
                env->FindClass(kClassMediaScannerClient);

        if (mediaScannerClientInterface == NULL) {
            ALOGE(Class %s not found, kClassMediaScannerClient);
        } else {
            mScanFileMethodID = env->GetMethodID(
                                    mediaScannerClientInterface,
                                    scanFile,
                                    (Ljava/lang/String;JJZZ)V);

            mHandleStringTagMethodID = env->GetMethodID(
                                    mediaScannerClientInterface,
                                    handleStringTag,
                                    (Ljava/lang/String;Ljava/lang/String;)V);

            mSetMimeTypeMethodID = env->GetMethodID(
                                    mediaScannerClientInterface,
                                    setMimeType,
                                    (Ljava/lang/String;)V);
        }

 

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved