1

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

我的课(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>
4

1 回答 1

1

如果您在使用 Camera Intent 时唯一感兴趣的是图像的缩小版本,您应该使用CameraManager#EXTRA_THUMBNAIL_FILE_PATH而不是完整图片,原因如下:

  1. 立即可用:由于一些后期处理,完整图像可能需要几秒钟才能写入
  2. 它已经按比例缩小到预览大小

如果您需要完整的图像,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();
    }
}
于 2014-05-19T16:50:07.303 回答