Google Glass文件观察程序无法正常运行

时间:2014-05-16 18:32:22

标签: android bitmap google-glass fileobserver

我的程序的目标是(至少在这个阶段)从玻璃上获取照片然后在屏幕上显示(我正在使用沉浸式)。

我的班级(当然extends Activity)有以下内容:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_picture_analysis);

    Intent getTheImageIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(getTheImageIntent, 25);
}

启动相机。用户接收并接受照片后,会调用我的public void onActivityResult方法

public void onActivityResult(int requestCode, int resultCode, Intent data) {



    // A bunch of debug logging that I inserted
    Log.d("glass", "got a pic!!");
    Log.d("glass", String.valueOf(resultCode));
    Log.d("glass", String.valueOf(resultCode==RESULT_OK));


    if (data.getExtras() != null && requestCode == 25) {
        Log.d("glass", "in the if");

        //Create an instance of bundle and get the returned data
        Bundle extras = data.getExtras();

        final String pathToImage = (String) extras.get("picture_file_path");

        for (String key : extras.keySet()) {
            Object value = extras.get(key);
            Log.d("glass", String.format("%s %s (%s)", key,
                    value.toString(), value.getClass().getName()));
        }




        FileObserver observer = new FileObserver(pathToImage) {

            @Override
            public void onEvent(int event, String path) {

                Log.d("glass", "we observed an event");

                if (event == FileObserver.CLOSE_WRITE || event==FileObserver.CLOSE_NOWRITE) {
                    Bitmap thePicUnscaled = BitmapFactory.decodeFile(pathToImage);

                    Log.d("glass", "observer found a close_write event");


                    Bitmap thePic = Bitmap.createScaledBitmap(thePicUnscaled,
                            getResources().getDisplayMetrics().widthPixels,
                            getResources().getDisplayMetrics().heightPixels,
                            true);

                    ImageView imgView = (ImageView) findViewById(R.id.thePictureView);
                    imgView.setImageBitmap(thePic);
                }
            }
        };

        observer.startWatching();
        Log.d("glass","observer started watching");


    } else {
        Log.d("glass", "There were no extras passed with intent in onActivityResult");
    }

}

现在我的问题是虽然我已经调用了文件观察者,但它实际上从未观察到变化。即使每次有事件我都应该记录Log.d("glass", "we observed an event");,但它永远不会记录。我的logcat(为我的标签过滤)看起来像:

05-16 14:27:31.007    4976-4976/com.nkhosla.glasstest.app D/dalvikvm﹕ Late-enabling CheckJNI
05-16 14:27:31.007    4976-4976/com.nkhosla.glasstest.app D/dalvikvm﹕ Try to disable coredump for pid 4976
05-16 14:27:31.007    4976-4976/com.nkhosla.glasstest.app D/dalvikvm﹕ Process 4976 nice name: com.nkhosla.glasstest.app
05-16 14:27:31.007    4976-4976/com.nkhosla.glasstest.app D/dalvikvm﹕ Extra Options: not specified
05-16 14:27:32.031    4976-4976/com.nkhosla.glasstest.app D/OpenGLRenderer﹕ Enabling debug mode 0
05-16 14:27:35.625    4976-4976/com.nkhosla.glasstest.app D/glass﹕ got a pic!!
05-16 14:27:35.625    4976-4976/com.nkhosla.glasstest.app D/glass﹕ -1
05-16 14:27:35.625    4976-4976/com.nkhosla.glasstest.app D/glass﹕ true
05-16 14:27:35.625    4976-4976/com.nkhosla.glasstest.app D/glass﹕ in the if
05-16 14:27:35.625    4976-4976/com.nkhosla.glasstest.app D/glass﹕ thumbnail_file_path /storage/emulated/0/thumbnail_cache/t_thumb_20140516_142732_930.jpg (java.lang.String)
05-16 14:27:35.625    4976-4976/com.nkhosla.glasstest.app D/glass﹕ picture_file_path /storage/emulated/0/DCIM/Camera/20140516_142732_930.jpg (java.lang.String)
05-16 14:27:35.640    4976-4976/com.nkhosla.glasstest.app D/glass﹕ observer started watching

我已经尝试在观察者开始观看之后立即访问该文件,但如果我尝试访问使用NullPointerException

创建的位图,我会得到Bitmap thePicUnscaled = BitmapFactory.decodeFile(pathToImage);

我让文件观察者坐了一下手表至少5秒左右,这是闪存写入速度的永恒。我不知道我的问题是什么。

作为参考,我的layout.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="com.nkhosla.glasstest.app.PictureAnalysis">


    <ImageView
        android:layout_height="fill_parent"
        android:layout_width="fill_parent"
        android:id="@+id/thePictureView"
        android:maxHeight="300dp"
        android:maxWidth="640dp"
        android:scaleType="centerCrop"
        android:adjustViewBounds="true"/>

</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

如果你在使用Camera Intent时唯一感兴趣的是图片的缩小版本,那么你应该使用CameraManager#EXTRA_THUMBNAIL_FILE_PATH代替完整图片,原因如下:

  1. 立即可用:由于某些后期处理,完整图像可能需要多秒才能写入
  2. 已缩小至预览尺寸
  3. 如果需要完整映像,FileObserver应该观察文件的父目录,而不是文件本身。 以下是我们在Developer's Guide中提供的代码段:

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