这是我的第一篇文章。如果有任何错误,请纠正我:)
我遇到了在图库中的特定文件夹中显示图像(通过我的应用程序捕获)的问题。我已设法将其保存在目录中的应用程序文件夹中,但是当我继续进入图库时,它将不显示我的应用程序的文件夹(首次创建时)和捕获的图像除非我重新启动我的手机
我在Android 4.4.4上运行并根据this,它似乎是一个错误?
无论如何,这是我的代码:
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");
}
}
我对这整件事情都很陌生(捕捉,存储等)并且已经尝试了好几天。任何帮助深表感谢。提前谢谢!