自定义照片文件夹显示在图库中

时间:2013-08-14 15:03:33

标签: android android-camera

我正在开发一款使用相机并将图片保存到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());
            }
        }
    }
}

1 个答案:

答案 0 :(得分:4)

更新图库应用。只需点击保存后,只需在保存后输入这个简单的代码。

            sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,
                Uri.parse("file://"
                        + Environment.getExternalStorageDirectory())));

此代码会告诉图库应用已添加内容,请立即重新扫描媒体:D。