Android照片库未使用新照片进行更新

时间:2012-08-03 21:08:37

标签: android

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

2 个答案:

答案 0 :(得分:0)

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

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

答案 1 :(得分:0)

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

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