[英]Why an Android may delete data saved under Media.EXTERNAL_CONTENT_URI?
Why an Android may delete data saved under Media.EXTERNAL_CONTENT_URI after unmount/mount SD card action? 为什么在卸载/挂载SD卡后,Android可以删除在Media.EXTERNAL_CONTENT_URI下保存的数据? How to avoid this? 如何避免这种情况?
I save ringtones using this URI and the default content resolver from context. 我使用此URI和上下文中的默认内容解析器保存了铃声。
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. 一切正常,但是在我卸载SD卡然后再次安装后,此铃声已不存在,因此无法在此表中找到。
Should I handle UNMOUNT event and backup somehow my saved data and restore it as soon as SD card is available again? 我应该处理UNMOUNT事件并以某种方式备份我保存的数据并在SD卡再次可用时将其还原吗?
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: 如果要在卸载/安装SD卡后保留媒体,则需要在保存新媒体后立即通知新媒体:
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. 如果这样做,在重新安装SD卡后,您的媒体仍然可用。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.