码迷,mamicode.com
首页 > 移动开发 > 详细

Android MediaScannerJNI源码详解

时间:2015-07-17 21:03:29      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:android   jni   源码   mediascann   

1.简介

MediaScannerJNI的在MediaScanner中的地位可参考
Android MediaScanner 总纲
MediaScanner JNI文件名:
android_media_MediaScanner.cpp
位置:
frameworks\base\media\jni\android_media_MediaScanner.cpp
android_media_MediaScanner是JAVA层和server层沟通的一个桥梁
技术分享
android_media_MediaScanner中有一个MyMediaScannerClient是一个调用JAVA层android/media/MediaScannerClient的入口是Native层想java层通信的桥梁。

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");
        }

版权声明:本文为博主原创文章,未经博主允许不得转载。

Android MediaScannerJNI源码详解

标签:android   jni   源码   mediascann   

原文地址:http://blog.csdn.net/matrix_laboratory/article/details/46931911

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!