Android照片库未更新新照片

Android照片库未更新新照片,android,Android,我有一个有两个主按钮的应用程序。一个打开一个图库,其中包含设备上的每张照片,另一个弹出相机。两者都很好,但问题是:当我用相机按钮拍照时,它不会显示在图库中!即使我关闭应用程序并再次打开它,也不会强迫库代码再次运行。我能让图片显示出来的唯一方法是通过usb将设备连接到我的电脑,然后断开连接,我想强制进行媒体搜索、更新之类的操作 以下是摄像机意图的代码: if (getApplicationContext().getPackageManager().hasSystemFeature(Package

我有一个有两个主按钮的应用程序。一个打开一个图库,其中包含设备上的每张照片,另一个弹出相机。两者都很好,但问题是:当我用相机按钮拍照时,它不会显示在图库中!即使我关闭应用程序并再次打开它,也不会强迫库代码再次运行。我能让图片显示出来的唯一方法是通过usb将设备连接到我的电脑,然后断开连接,我想强制进行媒体搜索、更新之类的操作

以下是摄像机意图的代码:

 if (getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA)){
                            Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
                             String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
                            File photo = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),  "IMG_" + timeStamp  + ".jpg");
                            intent.putExtra(MediaStore.EXTRA_OUTPUT,
                                    Uri.fromFile(photo));
                            imageUri = Uri.fromFile(photo);
                            startActivityForResult(intent, 1);

                        } else {

                            Toast.makeText(getApplicationContext(), "Seu dispositivo Android não possui uma câmera funcional.", Toast.LENGTH_LONG).show();
                        }
以下是图库的代码:

final String[] columns = { MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID };
        final String orderBy = MediaStore.Images.Media._ID;
        Cursor imagecursor = managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns, null,null, orderBy);
        int image_column_index = imagecursor.getColumnIndex(MediaStore.Images.Media._ID);
        int dataColumnIndex = imagecursor.getColumnIndex(MediaStore.Images.Media.DATA);
        this.count = imagecursor.getCount();
        this.thumbnails = new Bitmap[this.count];
        this.arrPath = new String[this.count];
        this.thumbnailsselection = new boolean[this.count];
        for (int i = 0; i < this.count; i++) {
            imagecursor.moveToPosition(i);
            int id = imagecursor.getInt(image_column_index);
            thumbnails[i] = MediaStore.Images.Thumbnails.getThumbnail(
                    getApplicationContext().getContentResolver(), id,
                    MediaStore.Images.Thumbnails.MICRO_KIND, null);
            arrPath[i]= imagecursor.getString(dataColumnIndex);
PS:我知道managedQuery已被弃用,但这是我知道的唯一方法。。。
谢谢

您可以尝试在写入文件后通知内容URI有更改:

getContentResolver().notifyChange(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null);

您可以尝试在写入文件后通知内容URI有更改:

getContentResolver().notifyChange(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null);

我解决我的问题后,把这个代码后,保存我的文件

 // to tell system update data
 sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));

我解决我的问题后,把这个代码后,保存我的文件

 // to tell system update data
 sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));

你需要打电话给MediaScanner。查看此问题:您需要致电MediaScanner。见这个问题: