Android Google Glass文件查看器工作不正常

Android Google Glass文件查看器工作不正常,android,bitmap,google-glass,fileobserver,Android,Bitmap,Google Glass,Fileobserver,我的程序的目标是(至少在这个阶段)从玻璃上获得一张照片,然后将其显示在屏幕上(我使用的是沉浸式) 我的类(扩展活动当然)有以下内容: protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_picture_analysis); Intent getTheImageIntent = ne

我的程序的目标是(至少在这个阶段)从玻璃上获得一张照片,然后将其显示在屏幕上(我使用的是沉浸式)

我的类(
扩展活动
当然)有以下内容:

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);
}
它启动了摄像机。用户拍摄并接受照片后,将调用my
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”,“我们观察到了一个事件”),它从不记录。我的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
我尝试在观察者开始观看后立即访问该文件,但如果我尝试访问使用位图thePicUnscaled=BitmapFactory.decodeFile(pathToImage)创建的位图,我会得到一个
NullPointerException

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

作为参考,my 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>

如果您在使用相机时唯一感兴趣的是图像的缩小版本,出于以下原因,您应该使用而不是完整的图片:

  • 它立即可用:由于某些后期处理,完整图像可能需要几秒钟才能写入
  • 它已经缩小到预览大小
  • 如果需要完整映像,FileObserver应该观察文件的父目录,而不是文件本身。 以下是我们在以下文档中提供的代码片段:

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