当我尝试将图像保存到目录时...该目录由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();
}
}
答案 0 :(得分:1)
我修复了添加文件的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