Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Updating gallery after taking pictures Gallery not showing all the pictures Android Camera

I am using a onClick over a button to capture an image.

PictureCallback myPictureCallback_JPG = new PictureCallback() {

    @Override
    public void onPictureTaken(byte[] arg0, Camera arg1) {

        FileOutputStream outStream = null;
        try {
            // Write to SD Card
            outStream = new FileOutputStream(String.format("/sdcard/%d.jpg", System.currentTimeMillis())); 
            outStream.write(arg0);
            outStream.close();
            Toast.makeText(Photo.this, "Image Saved to SD Card", Toast.LENGTH_SHORT).show();
            System.out.println();
        } catch (FileNotFoundException e) { 
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

        camera.startPreview();
    }};

The image gets saved in the SD card. Then the user clicks on Send button and a layout opens, on Click of ImageView an Image Gallery opens and clicking on a particular image, the URI gets selected.

    imageAttachPhoto = (ImageView)findViewById(R.id.imageViewPhotoOne);
    imageAttachPhoto.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {
            Intent intent = new Intent();
            intent.setType("image/*");
            intent.setAction(Intent.ACTION_GET_CONTENT);
            startActivityForResult(Intent.createChooser(intent,"Select Picture"), SELECT_PICTURE);
        }
    });
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT_OK) {
        if (requestCode == SELECT_PICTURE) {
            Uri selectedImageUri = data.getData();
            selectedImagePath = getPath(selectedImageUri);
            System.out.println("Image Path : " + selectedImagePath);
            imageAttachPhoto.setImageURI(selectedImageUri);
        }
    }
}

public String getPath(Uri uri) {
    String[] projection = { MediaStore.Images.Media.DATA };
    Cursor cursor = managedQuery(uri, projection, null, null, null);
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
}

But I am not able to view images snapped in Gallery code, but could see the images through FileBrowser and when I open the SD card in PC I can see the image and then the Android shows the image in Gallery also with in the code. Let me know what the issue is and how to solve it, looking forward to your reply. Thanks.

like image 722
Mukunda Avatar asked Dec 01 '25 08:12

Mukunda


1 Answers

Even if this is a little bit older! But I just had the same problem. Soni´s answer works but requires the Media Scanner every time to look for new files in the whole directory. You can also just register your new file that seems a bit more performant

sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://"+f.getAbsolutePath())));

f is the File you want to register.

like image 65
Vossi Avatar answered Dec 03 '25 23:12

Vossi



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!