Google Glass - 拍摄照片并以编程方式保存

时间:2014-07-08 12:38:29

标签: android google-glass android-camera-intent

我想在我的应用程序中启动相机意图拍照并将其保存到内部存储。我正在使用Google开发者页面Capturing images or video的代码。 在processPictureWhenReady方法中,我实现了以下代码来保存图片:

private void processPictureWhenReady(final String picturePath) {
    Log.v("path processPictureWhenReady ", " " + picturePath);
    final File pictureFile = new File(picturePath);

    if (pictureFile.exists()) {
        // The picture is ready; process it.
        try {

            Bitmap imageBitmap = BitmapFactory.decodeFile(picturePath);
            int w = imageBitmap.getWidth();
            int h = imageBitmap.getHeight();

            Bitmap bm2 = Bitmap.createScaledBitmap(imageBitmap, w / 2,
                    h / 2, true);

            imageBitmap = bm2.copy(bm2.getConfig(), true);

            MediaStore.Images.Media.insertImage(getContentResolver(),
                    imageBitmap, "test", "Test");

        } catch (Exception e) {
            Log.e("Exc", e.getMessage());
        }

    }

相机意图正在启动,然后我点击接受"拍照。但后来没有任何反应。我在onActivityResult方法中有一条日志消息,注意到该方法没有被调用。

2 个答案:

答案 0 :(得分:0)

这是一个已知问题。我也有同样的问题。我在此期间跟踪案例here

我见过人们尝试使用SurfaceView实现预览模式(我个人没有让它工作但它值得一试)。另请检查here是否存在类似问题。

答案 1 :(得分:0)

我用这种方法很适合我。

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


    }


    if (pictureFile.exists()) {

    } else {

        final File parentDirectory = pictureFile.getParentFile();
        FileObserver observer = new FileObserver(parentDirectory.getPath()) {
            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 = (event == FileObserver.CLOSE_WRITE && 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();
    }
}