【问题标题】:How does the GDK android camera example work?GDK android 相机示例如何工作?
【发布时间】:2015-02-21 18:27:47
【问题描述】:

所以我对这段代码(在processPictureWhenReady() 内部)末尾发生的事情有点困惑。在调用此方法之前(在onActivityResult() 内)我们有图像文件路径......所以文件已经被存储了。然后,我们使用图像的文件路径 (picturePath) 来声明文件 pictureFile。有人可以解释为什么我们必须使用相同的文件路径声明一个文件,尽管该文件已经存在?请不要只是将我转回 Google 开发者网站,因为我发现这没有用。

private static final int TAKE_PICTURE_REQUEST = 1;

private void takePicture() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, TAKE_PICTURE_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == TAKE_PICTURE_REQUEST && resultCode == RESULT_OK) {
        String thumbnailPath = data.getStringExtra(Intents.EXTRA_THUMBNAIL_FILE_PATH);
        String picturePath = data.getStringExtra(Intents.EXTRA_PICTURE_FILE_PATH);

        processPictureWhenReady(picturePath);
        // TODO: Show the thumbnail to the user while the full picture is being
        // processed.
    }

    super.onActivityResult(requestCode, resultCode, data);
}

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

    if (pictureFile.exists()) {
        // The picture is ready; process it.
    } 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();
}
}

【问题讨论】:

    标签: android google-glass google-gdk abstraction android-file


    【解决方案1】:

    我猜你指的是这一行:

    final File pictureFile = new File(picturePath);
    

    他们所做的是从存储该图像的文件路径创建一个File 对象的实例。这个File 对象是他们需要对图像进行一些处理的对象。 File 对象是内存中图像文件的抽象不是实际的物理文件。据我了解,您似乎误会了两者。

    【讨论】:

    • 除了“图像文件的抽象”之外,我遵循了您所说的一切。抱歉,您到底是什么意思?
    • 您的问题是,有人可以解释为什么我们必须使用相同的文件路径声明文件,尽管该文件已经存在。 在我看来,您是错误地认为pictureFile 是一个实际的文件。它是不是pictureFile 只是 File 类的一个实例,可用于对 actual 文件执行一些处理和操作。所以他们不会再次创建同一个文件,他们只是创建一个名为 pictureFile 的抽象表示,并使用它可以以编程方式对该图像执行一些操作:)
    • 这很有意义。谢谢 Zygotelnit!
    猜你喜欢
    • 1970-01-01
    • 2012-01-16
    • 2011-06-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多