【问题标题】:Android Camera Intent on Glass - Save image in custom file pathGlass上的Android相机意图 - 将图像保存在自定义文件路径中
【发布时间】:2014-01-06 23:27:09
【问题描述】:

我在尝试使用自定义文件名保存图像的 Android 活动中有以下代码。

我尝试从这篇文章的最佳答案中复制代码:Android - Taking photos and saving them with a custom name to a custom destination via Intent

我的代码如下:

    //camera stuff
    Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

    //folder stuff
    File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
    imagesFolder.mkdirs();
    File image = new File(imagesFolder, "QR_" + timeStamp + ".png");
    Uri uriSavedImage = Uri.fromFile(image);

    imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
    Log.d(LOG_TAG, "writing photo to: " + uriSavedImage.toString());
    startActivityForResult(imageIntent, 1);

当我运行这个活动时,它会在屏幕上显示一张图片,我必须点击触摸板才能接受(这是在 Glass 上运行的),它会在 DDMS 中打印以下内容:

writing photo to: file:///mnt/sdcard/MyImages/QR_20140106_181934.jpg

但是,当我通过 DDMS 在文件资源管理器中检查该目录时,它是空的。它成功创建了 MyImages 目录,但没有在该目录中保存任何文件。

相反,它使用我刚刚拍摄的图像创建了以下文件:/mnt/sdcard/DCIM/Camera/20140106_181935_635.jpg

所以它捕获了正确的图像,但只是忽略了我告诉它保存它的位置。任何想法我做错了什么?

【问题讨论】:

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


    【解决方案1】:

    文件可能没有写完整,所以使用FileWatcher,就像GDK docs中的那个:

    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 picturePath = data.getStringExtra(
                    CameraManager.EXTRA_PICTURE_FILE_PATH);
            processPictureWhenReady(picturePath);
        }
    
        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()) {
                // Protect against additional pending events after CLOSE_WRITE 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 = (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();
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2012-12-07
      • 1970-01-01
      • 2013-05-20
      • 2016-06-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多