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

android源码中,在系统多媒体数据库中增加一个字段

时间:2016-07-14 01:25:33      阅读:269      评论:0      收藏:0      [点我收藏+]

标签:

由于项目需求,在系统多媒体管理数据库里的存储图像文件的表中需要新增加一个字段,源码在:项目\packages\providers\MediaProvider\MediaProvider.java下,在updateDatabase()方法里,你会发现images表格是由files创建的一个视图,于是,在files的创建里增加了一个字段,如下:

....
 db.execSQL("CREATE TABLE files (" +
                        "_id INTEGER PRIMARY KEY AUTOINCREMENT," +
                        "_data TEXT," +     // this can be null for playlists
                        "_size INTEGER," +
                        "format INTEGER," +
                        "parent INTEGER," +
                        "date_added INTEGER," +
                        "date_modified INTEGER," +
                        "mime_type TEXT," +
                        "title TEXT," +
                        "description TEXT," +
                        "_display_name TEXT," +

                        // for images
                        "picasa_id TEXT," +
                        "orientation INTEGER," +

                        // for images and video
                        "latitude DOUBLE," +
                        "longitude DOUBLE," +
                        "datetaken INTEGER," +
                        "mini_thumb_magic INTEGER," +
                        "bucket_id TEXT," +
                        "bucket_display_name TEXT," +
                        "isprivate INTEGER," +
                        "is_upload INTEGER," + //----------新增字段

                        // for audio
                        "title_key TEXT," +
                        "artist_id INTEGER," +
                        "album_id INTEGER," +
                        "composer TEXT," +
                        "track INTEGER," +
                        "year INTEGER CHECK(year!=0)," +
                        "is_ringtone INTEGER," +
                        "is_music INTEGER," +
                        "is_alarm INTEGER," +
                        "is_notification INTEGER," +
                        "is_podcast INTEGER," +
                        "is_doorbell INTEGER," +
                        "is_auto_alarm INTEGER,"+
                        "album_artist TEXT," +

                        // for audio and video
                        "duration INTEGER," +
                        "bookmark INTEGER," +

                        // for video
                        "artist TEXT," +
                        "album TEXT," +
                        "resolution TEXT," +
                        "tags TEXT," +
                        "category TEXT," +
                        "language TEXT," +
                        "mini_thumb_data TEXT," +

                        // for playlists
                        "name TEXT," +

                        // media_type is used by the views to emulate the old
                        // images, audio_meta, videos and audio_playlist tables.
                        "media_type INTEGER," +

                        // Value of _id from the old media table.
                        // Used only for updating other tables during database upgrade.
                        "old_id INTEGER," +
                        "isaccurateduration INTEGER" +
                       ");");

  

在images视图创建时,也增加了该字段

private static final String IMAGE_COLUMNS =
                        "_data,_size,_display_name,mime_type,title,date_added," +
                        "date_modified,description,picasa_id,isprivate,latitude,longitude," +
                        "datetaken,orientation,mini_thumb_magic,bucket_id,bucket_display_name," +
                        "width,height,is_upload";

 编译后,把apk push到system/priv-app下一看,发现运行错误:

03-01 09:31:24.367: E/AndroidRuntime(1065): java.lang.RuntimeException: Unable to get provider com.android.providers.media.MediaProvider: android.database.sqlite.SQLiteException: no such column: drm_content_uri (code 1): , while compiling: CREATE VIEW audio_meta AS SELECT _id,_data,_display_name,_size,mime_type,date_added,is_drm,date_modified,title,title_key,duration,artist_id,composer,album_id,track,year,is_ringtone,is_music,is_alarm,is_notification,is_podcast,is_doorbell,is_auto_alarm,bookmark,album_artist,drm_content_uri,drm_offset,drm_dataLen,drm_rights_issuer,drm_content_name,drm_content_description,drm_content_vendor,drm_icon_uri,drm_method,title_pinyin_key FROM files WHERE media_type=2;
03-01 09:31:24.367: E/AndroidRuntime(1065): at android.app.ActivityThread.installProvider(ActivityThread.java:5011)
03-01 09:31:24.367: E/AndroidRuntime(1065): at android.app.ActivityThread.installContentProviders(ActivityThread.java:4582)
03-01 09:31:24.367: E/AndroidRuntime(1065): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4522)
03-01 09:31:24.367: E/AndroidRuntime(1065): at android.app.ActivityThread.access$1500(ActivityThread.java:151)
03-01 09:31:24.367: E/AndroidRuntime(1065): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1381)
03-01 09:31:24.367: E/AndroidRuntime(1065): at android.os.Handler.dispatchMessage(Handler.java:110)
03-01 09:31:24.367: E/AndroidRuntime(1065): at android.os.Looper.loop(Looper.java:193)
03-01 09:31:24.367: E/AndroidRuntime(1065): at android.app.ActivityThread.main(ActivityThread.java:5292)
03-01 09:31:24.367: E/AndroidRuntime(1065): at java.lang.reflect.Method.invokeNative(Native Method)
03-01 09:31:24.367: E/AndroidRuntime(1065): at java.lang.reflect.Method.invoke(Method.java:515)
03-01 09:31:24.367: E/AndroidRuntime(1065): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:824)
03-01 09:31:24.367: E/AndroidRuntime(1065): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:640)
03-01 09:31:24.367: E/AndroidRuntime(1065): at dalvik.system.NativeStart.main(Native Method)
03-01 09:31:24.367: E/AndroidRuntime(1065): Caused by: android.database.sqlite.SQLiteException: no such column: drm_content_uri (code 1): , while compiling: CREATE VIEW audio_meta AS SELECT _id,_data,_display_name,_size,mime_type,date_added,is_drm,date_modified,title,title_key,duration,artist_id,composer,album_id,track,year,is_ringtone,is_music,is_alarm,is_notification,is_podcast,is_doorbell,is_auto_alarm,bookmark,album_artist,drm_content_uri,drm_offset,drm_dataLen,drm_rights_issuer,drm_content_name,drm_content_description,drm_content_vendor,drm_icon_uri,drm_method,title_pinyin_key FROM files WHERE media_type=2;
03-01 09:31:24.367: E/AndroidRuntime(1065): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
03-01 09:31:24.367: E/AndroidRuntime(1065): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:893)
03-01 09:31:24.367: E/AndroidRuntime(1065): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:504)
03-01 09:31:24.367: E/AndroidRuntime(1065): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
03-01 09:31:24.367: E/AndroidRuntime(1065): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
03-01 09:31:24.367: E/AndroidRuntime(1065): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
03-01 09:31:24.367: E/AndroidRuntime(1065): at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1697)
03-01 09:31:24.367: E/AndroidRuntime(1065): at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1628)
03-01 09:31:24.367: E/AndroidRuntime(1065): at com.android.providers.media.MediaProvider.updateViewAfterNewTable(MediaProvider.java:7404)
03-01 09:31:24.367: E/AndroidRuntime(1065): at com.android.providers.media.MediaProvider.updateDatabase(MediaProvider.java:2264)
03-01 09:31:24.367: E/AndroidRuntime(1065): at com.android.providers.media.MediaProvider.access$1200(MediaProvider.java:126)
03-01 09:31:24.367: E/AndroidRuntime(1065): at com.android.providers.media.MediaProvider$DatabaseHelper.onCreate(MediaProvider.java:493)
03-01 09:31:24.367: E/AndroidRuntime(1065): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
03-01 09:31:24.367: E/AndroidRuntime(1065): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
03-01 09:31:24.367: E/AndroidRuntime(1065): at com.android.providers.media.MediaProvider.query(MediaProvider.java:2796)
03-01 09:31:24.367: E/AndroidRuntime(1065): at com.android.providers.media.MediaProvider.attachVolume(MediaProvider.java:6391)
03-01 09:31:24.367: E/AndroidRuntime(1065): at com.android.providers.media.MediaProvider.onCreate(MediaProvider.java:889)
03-01 09:31:24.367: E/AndroidRuntime(1065): at android.content.ContentProvider.attachInfo(ContentProvider.java:1638)
03-01 09:31:24.367: E/AndroidRuntime(1065): at android.content.ContentProvider.attachInfo(ContentProvider.java:1609)
03-01 09:31:24.367: E/AndroidRuntime(1065): at android.app.ActivityThread.installProvider(ActivityThread.java:5008)

好吧,说files里没有字段“is_upload”,这是为什么呢?反复看了updateDatabase()的代码,发现有这么一段:

db.execSQL("INSERT OR REPLACE INTO files2 SELECT " + getAllColumns() + " FROM files;");
            db.execSQL("DROP TABLE files;");
            db.execSQL("ALTER TABLE files2 RENAME TO files;");

  原来,files的源头是files2,那么在files2创建的时候加入“is_upload”字段,并且在getAllColumns()里加入该字段,即可。

编译验证,OK!

android源码中,在系统多媒体数据库中增加一个字段

标签:

原文地址:http://www.cnblogs.com/suxiaoqi/p/5668085.html

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