我正在开发一个使用相机并将图片保存到 DCIM/MyAppFolder 的应用程序。我的第一个问题是我应该将它保存到 DCIM 还是图片?如果它甚至很重要。我的第二个问题是我注意到文件夹/图片需要一段时间才能显示在本机图库应用程序中。在使用其他应用程序(Instagram、Snapseed 等)进行测试后,这些照片会立即出现。我是否缺少一段代码来完成此操作?我的代码如下:
public void takePhoto() {
Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
//folder stuff
File imagesFolder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "MyAppFolder");
imagesFolder.mkdirs();
//String filePath = "/MyImages/QR_" + timeStamp + ".png" ;
File image = new File(imagesFolder, "IMG_" + timeStamp + ".jpg");
imageUri = Uri.fromFile(image);
imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(imageIntent, TAKE_PICTURE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case 1:
if (resultCode == Activity.RESULT_OK) {
Uri selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
ContentResolver cr = getContentResolver();
Bitmap bitmap;
try {
bitmap = android.provider.MediaStore.Images.Media
.getBitmap(cr, selectedImage);
Toast.makeText(this, selectedImage.toString(),
Toast.LENGTH_LONG).show();
} catch (Exception e) {
Toast.makeText(this, "Failed to load", Toast.LENGTH_SHORT)
.show();
Log.e("Camera", e.toString());
}
}
}
}