user3195396
user3195396

Reputation: 254

Any picture that I took does saves on my sd card but unable to view it unless rebooted

Any pictures that I took does not appears in the gallery and even in the sd card, but it saves nonetheless. All I need to do is to reboot the system and there it is.

Here's my current code:

PictureCallback myPictureCallback_JPG = new PictureCallback(){

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

        FileOutputStream outStream = null;

        try 
        {
            File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "myCaptured");

            if (!mediaStorageDir.exists()) 
            {
                if (!mediaStorageDir.mkdirs()) 
                {
                    Log.d("myCaptured", "Oops! Failed create " + "myCaptured" + " directory");
                }
            }

            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date());

            String path = mediaStorageDir.getPath() + File.separator + "IMG_" + timeStamp + ".jpg";
            outStream = new FileOutputStream(String.format(path, System.currentTimeMillis()));
            outStream.write(arg0);
            outStream.close();

        } 
        catch (FileNotFoundException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        finally 
        {
            Toast.makeText(getApplicationContext(), "Image Saved", Toast.LENGTH_SHORT).show();
            VuzixCamera.super.onBackPressed();
        }

        camera.startPreview();
    }};

Upvotes: 0

Views: 64

Answers (2)

CommonsWare
CommonsWare

Reputation: 1006674

You need to use MediaScannerConnection and scanFile() to get the file indexed, before it will be visible to the Gallery app, MTP clients (e.g., Windows desktops), etc.

Upvotes: 2

Nish8900
Nish8900

Reputation: 82

 public void onPictureTaken(byte[] data, Camera camera) {
Uri imageFileUri = getContentResolver().insert(
    Media.EXTERNAL_CONTENT_URI, new ContentValues());
try {
  OutputStream imageFileOS = getContentResolver().openOutputStream(
      imageFileUri);
  imageFileOS.write(data);
  imageFileOS.flush();
  imageFileOS.close();

  Toast t = Toast.makeText(this, "Saved JPEG!", Toast.LENGTH_SHORT);
  t.show();
} catch (Exception e) {
  Toast t = Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT);
  t.show();
}
camera.startPreview();

}}

Also, provide it in your manifest:-

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

-----Use above code it will save the image in SD card-----

Upvotes: 2

Related Questions