2

我有一个带有两个主要按钮的应用程序。一个打开一个包含设备上每张照片的画廊,另一个弹出相机。两者都工作正常,但问题是:当我用相机按钮拍照时,它不会显示在图库中!即使我关闭应用程序并再次打开它(强制库的代码再次运行)也不会。我可以显示图片的唯一方法是通过 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 已被弃用,但这是我知道该怎么做的唯一方法......谢谢!

4

2 回答 2

0

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

getContentResolver().notifyChange(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null);
于 2012-08-03T21:20:59.750 回答
0

保存文件后输入此代码后,我解决了我的问题

 // to tell system update data
 sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
于 2018-12-19T08:23:11.693 回答