Camera Intent复制到自定义文件夹延迟

时间:2015-02-06 01:36:31

标签: android google-glass google-gdk

我在谷歌眼镜中有一个相机应用程序,我正在做的是,我正在将图像从默认目录复制到我的自定义目录。起初,我认为它不起作用,但当我关闭玻璃并再次打开时,所有照片都存在。这就是为什么我意识到它正在工作,但它需要重新启动才能发生变化。

这是我的代码。

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

我错过了什么吗?请帮我。谢谢。

1 个答案:

答案 0 :(得分:2)

我不确定你的意思"我认为它不起作用"但您可能需要发送广播以让图库知道您添加了新图片。

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

请参阅Image, saved to sdcard, doesn't appear in Android's Gallery apphow to update gallery after moving photo programmatically?

您还可以从shell检查图像是否存在。

注意:正如Ralfh评论的那样, 尝试使用Intent.ACTION_MEDIA_SCANNER_SCAN_FILE而不是Intent.ACTION_MEDIA_MOUNTED。