【问题标题】:Google Glass - Take a picture and save it programmaticallyGoogle Glass - 拍照并以编程方式保存
【发布时间】:2016-02-01 17:09:12
【问题描述】:

我想在我的应用中启动相机意图来拍照并将其保存到内部存储中。我正在使用谷歌开发者页面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 方法中有一条日志消息,并注意到该方法没有被调用。

【问题讨论】:

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


    【解决方案1】:

    这是一个已知问题。我也有同样的问题。我正在关注here的案例

    我看到人们尝试使用 SurfaceView 实现预览模式(我个人还没有让它工作,但值得一试)。也请检查here 是否有类似问题。

    【讨论】:

      【解决方案2】:

      我使用了这种方法,它对我很有效。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2010-12-11
        • 2012-03-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多