Android的照片库与新照片更新 [英] Android photo gallery not updating with new photos

查看:141
本文介绍了Android的照片库与新照片更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个主要按钮的应用程序。一开一家画廊与设备上的每一张照片,另一个弹出的摄像头。两者都工作正常,但这里的事:当我拍照与摄像头按钮,它不显示在画廊!甚至,如果我关闭应用程序,然后再次打开它(强制code为画廊再次运行)。我能得到的图片展现出来的唯一方法是通过USB设备连接到电脑上,然后断开(我想迫使媒体搜索,更新,类似的东西。)

I have an app with two main buttons. One open a gallery with every photo on the device and the other one pops up the camera. Both are working fine but here is the thing: When I take a picture with the camera button it doesn't show on the gallery! Not even if I close the app and open it again (forcing the code for the gallery to run again). The only way I can get the picture to show up is to connect the device to my computer via usb and then disconnect (I think forcing a media search, update, something like that.)

下面是code相机意图:

Here is the code for the camera intent:

 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();
                        }

下面是code为画廊:

Here is the code for the gallery:

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是德precated,但它是我知道怎么做的唯一途径...
谢谢!

PS: I know that managedQuery is deprecated, but it is the only way I know how to do... Thanks!

推荐答案

您可以尝试通知内容URI,有一个变化,你写一个文件后:

You can try notifying the content URI that there was a change after you write a file:

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

这篇关于Android的照片库与新照片更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆