Search code examples
androidgoogle-glassgoogle-gdk

Camera Intent copying to custom folder delayed


I have a camera application in google glass and what I'm doing is, I'm copying images from default directory to my custom directory. At first, I thought it's not working, but when I turn off the glass and turn it on again, all the pictures exists. That's why I realize that it's working but it needs a reboot to occur the changes.

This is my code.

public class CameraActivity extends Activity {

// Camera activity request codes
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 1;
private Uri fileUri;

private Slider mSlider;
private Slider.Indeterminate mIndeterminate;
private String TAG = "TAG";
private String pid;
private static final int MEDIA_TYPE_IMAGE = 1;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent = getIntent();
    pid = intent.getStringExtra("patientid");
    takePicture();
}

/**
 * Launching camera app to capture image
 */
private void takePicture() {

    // create Intent to take a picture and return control to the calling application
    Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}


/**
 * Receiving activity result method will be called after closing the camera
 * */
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // if the result is capturing Image
           if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
                View view1 = new CardBuilder(getBaseContext(), CardBuilder.Layout.MENU)
                .setText("Please Wait...")
                .setIcon(R.drawable.ic_photo_50)
                .getView();

                setContentView(view1);
                // Set the view for the Slider
                mSlider = Slider.from(view1);
                mIndeterminate = mSlider.startIndeterminate();
            String picturePath = data.getStringExtra(Intents.EXTRA_PICTURE_FILE_PATH);
                processPictureWhenReady(picturePath);

        } else if (resultCode == RESULT_CANCELED) {
            // user cancelled Image capture
            View view2 = new CardBuilder(getBaseContext(), CardBuilder.Layout.MENU)
            .setText("Cancelled")
            .setIcon(R.drawable.ic_no_50)
            .getView();

            setContentView(view2);
            AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
            am.playSoundEffect(Sounds.DISMISSED);
            finish();

        } else {
            // failed to capture image
            Toast.makeText(getApplicationContext(),
                    "Sorry Failed to capture image", Toast.LENGTH_SHORT)
                    .show();

            finish();
        }

    super.onActivityResult(requestCode, resultCode, data);

}

  private void processPictureWhenReady(final String picturePath) {
    final File pictureFile = new File(picturePath);

    if (pictureFile.exists()) {
        File from = new File(pictureFile.toString());
        File to = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "/ProfilePicture_"+ pid +".jpg");
        Log.d(TAG,to.toString());
        try {
            FileUtils.copyFile(from, to, true);            
            Log.d(TAG,"NO ERROR");
        } catch (IOException e) {            
            Log.d(TAG,"ERROR");
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        /* The picture is ready; process it.
        mIndeterminate.hide();
        String isImage = "Yes";
        Intent i = new Intent(CameraActivity.this, UploadActivity.class);
        i.putExtra("filePath", picturePath);
        i.putExtra("isImage", isImage);
        startActivity(i);*/
        finish();
    } else {
        // The file does not exist yet. Before starting the file observer, you
        // can update your UI to let the user know that the application is
        // waiting for the picture (for example, by displaying the thumbnail
        // image and a progress indicator).

        final File parentDirectory = pictureFile.getParentFile();
        FileObserver observer = new FileObserver(parentDirectory.getPath(),
                FileObserver.CLOSE_WRITE | FileObserver.MOVED_TO) {
            // Protect against additional pending events after CLOSE_WRITE
            // or MOVED_TO is handled.
            private boolean isFileWritten;

            @Override
            public void onEvent(int event, String path) {
                if (!isFileWritten) {
                    // For safety, make sure that the file that was created in
                    // the directory is actually the one that we're expecting.
                    File affectedFile = new File(parentDirectory, path);
                    isFileWritten = affectedFile.equals(pictureFile);

                    if (isFileWritten) {
                        stopWatching();

                        // Now that the file is ready, recursively call
                        // processPictureWhenReady again (on the UI thread).
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {

                                    processPictureWhenReady(picturePath);

                            }
                        });
                    }
                }
            }
        };
        observer.startWatching();
    }
}

Am I missing something? Please help me. Thank you.


Solution

  • I am not sure what you mean by "I thought it's not working" but you might need to send a broadcast to let the gallery know that you added new images.

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

    Please see Image, saved to sdcard, doesn't appear in Android's Gallery app and how to update gallery after moving photo programmatically?.

    You can also check from the shell if the images are there.

    Note: as Ralfh comment, try Intent.ACTION_MEDIA_SCANNER_SCAN_FILE instead of Intent.ACTION_MEDIA_MOUNTED.