2

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(AudikoFileStorageAccessor.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?

1 Answer 1

1

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.