Why an Android may delete data saved under Media.EXTERNAL_CONTENT_URI after unmount/mount SD card action? How to avoid this?
I save ringtones using this URI and the default content resolver from context.
The code is similar to that:
ContentValues values = new ContentValues();
values.put(Media.DATA, AudikoFileStorageAccessor.getInstance().getAbsolutePathForRingtone(ringtone.getId()));
values.put(Media.TITLE, ringtone.mSong);
values.put(Media.DISPLAY_NAME, ringtone.mSong);
values.put(Media.ARTIST, ringtone.mArtist);
values.put(Media.MIME_TYPE, "audio/mpeg");
values.put(Media.SIZE, ringtone.mSize * 1024);
values.put(Media.IS_RINGTONE, (RingtoneManager.TYPE_RINGTONE == type || type == 0));
values.put(Media.IS_NOTIFICATION, (RingtoneManager.TYPE_NOTIFICATION == type));
values.put(Media.IS_ALARM, (RingtoneManager.TYPE_ALARM == type));
values.put(Media.IS_MUSIC, false);
Uri newUri = mContext.getContentResolver().insert(
Media.getContentUriForPath(AudikoFileStorageAcc开发者_高级运维essor.getInstance().getAbsolutePathForRingtone(
ringtone.getId())), values);
RingtoneManager.setActualDefaultRingtoneUri(mContext, type, newUri);
Everything works fine, but after I unmount SD card and then mount it again this ringtone doesn't exist anymore and couldn't be found within this table.
Should I handle UNMOUNT event and backup somehow my saved data and restore it as soon as SD card is available again?
I've found the mistake. Hope someone would find this useful. If you want to keep your media after SD card was unmounted/mounted you need to notify about new media like that just after you save it:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, newUri));
If you do that, after you mount SD card back again your media will be still available.
精彩评论