简体   繁体   English

为什么Android可以删除Media.EXTERNAL_CONTENT_URI下保存的数据?

[英]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.

相关问题 Android中EXTERNAL_CONTENT_URI的文件路径 - Filepath of EXTERNAL_CONTENT_URI in Android “resolveUri在错误的位图uri上失败:content:// media / external / images / media / 49644”我无法查看图像 - “resolveUri failed on bad bitmap uri: content://media/external/images/media/49644” Im unable to view the image EXTERNAL_CONTENT_URI和INTERNAL_CONTENT_URI都从Android的内部存储卡中提取视频 - EXTERNAL_CONTENT_URI and INTERNAL_CONTENT_URI both fetch videos from internal memory card in android api 29+ 已弃用 MediaStore.Audio.Media.EXTERNAL_CONTENT_URI? - MediaStore.Audio.Media.EXTERNAL_CONTENT_URI is deprecated for api 29+? Android将内容URI转换为文件URI - Android converting content URI to file URI 保存在外部缓存中的文件在卸载时不会删除 - files saved in External Cache do not delete on uninstall 如何在Android中将`content:// media / external / images / media / Y`转换为`file:/// storage / sdcard0 / Pictures / X.jpg`? - how to convert `content://media/external/images/media/Y` to `file:///storage/sdcard0/Pictures/X.jpg` in android? Android 11:主目录(无效)不允许内容://媒体/外部/文件允许的目录是[下载,文档] - Android 11: Primary directory (invalid) not allowed for content://media/external/file allowed directories are [Download, Documents] 如何防止使用媒体存储外部 uri 扫描路径 - how to prevent a path to be scanned with media store external uri Android中Content Provider Uri中的“ content://”是否可以替换? - is “content://” in the Uri of Content Provider in Android replaceable ?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM