gpt4 book ai didi

java - 即使在使用 MediaScannerConnection.scanFile() 扫描后图像也没有显示在图库中

转载 作者:行者123 更新时间:2023-11-30 00:32:23 28 4
gpt4 key购买 nike

当我尝试将图像保存到外部目录时,MediaScannerConnection 成功扫描了该目录,但图像未显示在图库中。

public void saveItem() {
if (selectCount == 0) {
Toast.makeText(getActivity(), "Select at least one image", Toast.LENGTH_SHORT).show();
} else {
Iterator iterator = selectedFile.iterator();
while (iterator.hasNext()) {

gridFilePath = new File(iterator.next().toString());
String destinationPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/myImages/";
File destination = new File(destinationPath);
try {
FileUtils.copyFileToDirectory(gridFilePath, destination);
MediaScannerConnection.scanFile(getActivity(), new String[]{destinationPath},
null, new MediaScannerConnection.MediaScannerConnectionClient() {
@Override
public void onMediaScannerConnected() {

}

@Override
public void onScanCompleted(String path, Uri uri) {
Log.d("Scan","Scanning Completed");
}
}

);
Log.d("Image Saved", "Saved");
} catch (IOException e) {
e.printStackTrace();
}
}

Toast.makeText(getActivity(), "Pictures Saved", Toast.LENGTH_LONG).show();
}
}

最佳答案

我通过添加文件的 mimeType 解决了我的问题:

private void notifyNewFileToSystem(File file) {
String type = null;
String extension = MimeTypeMap.getFileExtensionFromUrl(file.getAbsolutePath());
if (extension != null) {
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
}

MediaScannerConnection.scanFile(getApplicationContext(),
new String[]{file.getAbsolutePath()},
new String[]{type},
(path, uri) -> {
Log.e(TAG, "Path: " + path);
Log.e(TAG, "Uri: " + uri);
}
);
}

我在这里找到了获取 mimeType 的解决方案: https://stackoverflow.com/a/8591230/2077248

关于java - 即使在使用 MediaScannerConnection.scanFile() 扫描后图像也没有显示在图库中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44053508/

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