这是我的第一篇文章。如有错误请指正:)
我在画廊的特定文件夹中显示图像(通过我的应用程序捕获)时遇到问题。我已经设法将它保存在目录中我的应用程序文件夹中,但是当我进入图库时,它不会显示我的应用程序文件夹(首次创建时)和捕获的图像,除非我重新启动手机。
我在 Android 4.4.4 上运行,根据这个,这似乎是一个错误?
无论如何,这是我的代码:
private void savePic() {
dir_path = "TravelBuddy";
travelBuddyDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), dir_path);
timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
imgName = (timeStamp + ".jpg");
output = null;
File imagePath = new File(travelBuddyDir, imgName);
try {
output = new FileOutputStream(imagePath);
bmp.compress(Bitmap.CompressFormat.JPEG, 100, output);
output.flush();
output.close();
MediaStore.Images.Media.insertImage(getActivity().getContentResolver(), bmp,
imgName, imgName);
} catch (Exception e) {
e.printStackTrace();
}
}
其中上面的方法会在下面的 onActivityResult(int requestCode, int resultCode, Intent data) 方法中被调用:
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (resultCode) {
case 1: {
if (resultCode == RESULT_OK) {
savePic();
}
break;
}
这是我在保存捕获的图像之前检查和创建目录的方法:(结果路径:/storage/emulated/0/Pictures/TravelBuddy)
private void checkDir() {
if(Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
dir_path = "TravelBuddy";
travelBuddyDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), dir_path);
if (!travelBuddyDir.exists() && (!travelBuddyDir.isDirectory())) {
travelBuddyDir.mkdir();
System.out.println("New directory has been created");
} else {
System.out.println("Directory already exists!");
}
} else {
System.out.println("No storage available");
}
}
我对整个事情(捕获,存储等)都是新手,并且已经尝试了好几天。任何帮助深表感谢。先感谢您!