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