2

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.

Dan Lowe
  • 51,713
  • 20
  • 123
  • 112
Mukunda
  • 1,593
  • 3
  • 26
  • 45

3 Answers3

9

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.

Vossi
  • 231
  • 2
  • 8
  • 1
    This is great. By the way, Instead of Uri.parse() you can also directly use Uri.fromFile(f) . Looks a bit nicer and does the same thing. :) – ghost23 May 19 '13 at 15:28
  • Just what I was looking for, much faster than scanning the whole directory. I'm using the _fromFile_ method and it is working well. – mez.pahlan Sep 13 '13 at 23:24
  • Uri.fromFile(file) it's even better – Alessandro Roaro Apr 24 '14 at 08:47
  • This is deprecated, callers should migrate to inserting items directly into MediaStore https://stackoverflow.com/questions/57726896/mediastore-images-media-insertimage-deprecated – zeitgeist Mar 04 '22 at 09:20
6

Android scan media file and those are shown when using content provider...when your run your app and take new camera at that time you just took new pics but default gallery doesn't know about that new pics cause they are not in content provider's list of media.

I think adding this line after taking picture and before calling gallery will show the all latest pics taken from camera.

    sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, 
                 Uri.parse("file://"+ Environment.getExternalStorageDirectory())));

This will throw a SecurityException in KitKat+. Try Vossi's method, or use MediaScannerConnection as used here: stackoverflow – @absk

Community
  • 1
  • 1
MKJParekh
  • 34,073
  • 11
  • 87
  • 98
  • 1
    plus one absolutely perfect...got it working...but whenever I want to take image in portrait mode the orientation will be rotated by 90 degrees. Any idea on how to set it right. thanks. – Mukunda Dec 29 '11 at 12:36
  • hmm..not getting you exactly cause you are calling default intent..anyway..if not finiding solution over internet then put another question on SO and people will help you. – MKJParekh Dec 29 '11 at 12:45
  • 1
    This will throw a SecurityException in KitKat+. Try Vossi's method, or use MediaScannerConnection as used here: http://stackoverflow.com/questions/18624235/android-refreshing-the-gallery-after-saving-new-images – absk Jun 20 '14 at 06:14
  • `java.lang.SecurityException: Permission Denial: not allowed to send broadcast android.intent.action.MEDIA_MOUNTED from pid=11300, uid=10512` – Pratik Butani Apr 26 '16 at 19:44
  • @PratikButani That's an issue in KitKat only I guess , Check this answer for you http://stackoverflow.com/questions/24072489/java-lang-securityexception-permission-denial-not-allowed-to-send-broadcast-an – MKJParekh Apr 27 '16 at 05:49
-2
  1. simple answer is that first run the application, capture the image from camera of your application then simple see in gallery of your device.

  2. you can't see last capture image image by your application at that time one time restart your device and can see that your last capture image is in gallery of your device.

  3. one more thing, after your device restart then again open your own application and again capture one image from your application and see that your second capture image can see in gallery without any action in your device.