My technology, my musings, my life…
17 Mar
I’ve decided that from now on, along with all my Musical updates, I’m going to post some useful advice/tips/code/etc to help others solve any problems I might have encountered during the development of the update. So, here is the first of hopefully many “Android Development Details”
Today’s One Liner:
Get the current “android:versionCode” from AndroidManifest.xml:
getPackageManager().getPackageInfo(getPackageName(), 0).versionCode
Problem/Solution:
Once a media file has been detected on the SD card by Android’s MediaScanner, it is automatically to the MediaStore. The MediaStore is a SQL database used by all media-based applications instead of directly accessing the file system. Unfortunately, once an item has been added to the MediaStore, it is not immediately automatically removed when you delete the original file off the file system. This has to manually be done in the database. The easiest way I could find to do this is to get access to the Content Provider with getContentResolver() and call the delete() method with the arguments MediaStore.Audio.Media.EXTERNAL_CONTENT_URI (external because it is on the SD card) and a SQL WHERE clause indicating which file to remove. In my case, I needed the WHERE clause to find a specific file based on its path on the file system (since I had just deleted the original based on its path). The unix absolute path is stored in the MediaStore.Audio.Media.DATA (Note: this is a constant not a String–it must be concatenated into the SQL WHERE clause) column in the MediaStore database.
Hopefully these tips are helpful to some other Android developers, and as always, feel free to email me with any questions!