2

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.)

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

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: I know that managedQuery is deprecated, but it is the only way I know how to do... Thanks!

  • 1
    You need to call the MediaScanner. See this question: http://stackoverflow.com/questions/11581013/pictures-wont-save-on-samsung-nexus/11581631#11581631 – Krylez Aug 03 '12 at 21:27

2 Answers2

0

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);
Error 454
  • 7,255
  • 2
  • 33
  • 48
0

I solve my problem after put this code after save my file

 // to tell system update data
 sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
atline
  • 28,355
  • 16
  • 77
  • 113
Yudi karma
  • 324
  • 3
  • 15