我正在尝试让我的Android应用程序打开相机应用程序并允许用户拍摄一系列照片,然后在退出时,我想使用ContentObserver
来查询MediaStore.Images.Media.EXTERNAL_CONTENT_URI
找到用户拍摄的照片。
然而,问题是即使在拍摄一系列照片并点击后退按钮后,PhotoObserver.onChange
也永远不会被调用。
这是我的代码:
public class MainActivity extends AppCompatActivity {
PhotoObserver photoObserver = new PhotoObserver(new Handler());
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getApplicationContext()
.getContentResolver()
.registerContentObserver(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, false, photoObserver);
}
@Override
protected void onDestroy() {
super.onDestroy();
this.getApplicationContext()
.getContentResolver()
.unregisterContentObserver(photoObserver);
}
// Called when a button is hit!
public void takePicture(View view) {
Intent cameraIntent = new Intent(MediaStore.INTENT_ACTION_STILL_IMAGE_CAMERA);
startActivity(cameraIntent);
}
public class PhotoObserver extends ContentObserver {
public PhotoObserver(Handler handler) {
super(handler);
}
@Override
public void onChange(boolean selfChange) {
onChange(selfChange, null);
}
@Override
public void onChange(boolean selfChange, Uri uri) {
super.onChange(selfChange, uri);
// This never gets called!
System.out.println("onChange called");
}
}
}
非常感谢任何帮助!