gpt4 book ai didi

android - 存储访问框架不更新 MediaScanner (MTP)

转载 作者:太空狗 更新时间:2023-10-29 15:06:23 27 4
gpt4 key购买 nike

使用 KitKat 中提供的 SAF,不会对保存到设备内部或外部存储点的文件调用 MediaScanner。因此,我必须根据返回的 URI 来确定是否应该尝试运行 MediaScanner。

// The SAF uses content URI to pass meta about the file. The following host is used for internal storage.
if (mExportServiceUri.getHost().equals("com.android.externalstorage.documents")) {
final File externalStorage = Environment.getExternalStorageDirectory();
final String path = mExportServiceUri.getEncodedPath().replace("/document/primary%3A", "");
MediaScannerConnection.scanFile(mService.getApplicationContext(), new String[] { new File(
externalStorage, path).getAbsolutePath() }, null, null);
}

是否有其他人必须解决此问题?如果是,是否有比这更好的方法?目前只支持设备外接存储,SDCard等额外存储空间需要单独检查。

最佳答案

为了支持我认为是所有可能的安装,包括通过 OTG 连接的 USB 拇指驱动器,甚至可能直接连接到某些平板电脑上的全尺寸 USB 端口(我没有平板电脑来测试,是否存在 4.4 平板电脑有全尺寸端口?)我有以下似乎在 Galaxy S4(Play 商店版)和 N5 上运行良好。

// The SAF uses content URI to pass meta about the file. The following host is used for SD storage.
if (mExportServiceUri.getHost().equals("com.android.externalstorage.documents")) {
final String encodedPath = mExportServiceUri.getEncodedPath();
final String path = encodedPath.substring(encodedPath.indexOf("%3A") + 3);
final File[] storagePoints = new File("/storage").listFiles();

// document/primary is in /storage/emulated/legacy and thus will fail the exists check in the else handling loop check
if (encodedPath.startsWith("/document/primary")) {
// External file stored in Environment path
final File externalFile = new File(Environment.getExternalStorageDirectory(), path);
MediaScannerConnection.scanFile(mService.getApplicationContext(),
new String[] { externalFile.getAbsolutePath() }, null, null);
} else {
// External file stored in one of the mount points, check each mount point for the file
for (int i = 0, j = storagePoints.length; i < j; ++i) {
final File externalFile = new File(storagePoints[i], path);
if (externalFile.exists()) {
MediaScannerConnection.scanFile(mService.getApplicationContext(),
new String[] { externalFile.getAbsolutePath() }, null, null);
break;
}
}
}
}

关于android - 存储访问框架不更新 MediaScanner (MTP),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21605493/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com