5
我試圖站出一個簡單的Android應用程序,該應用程序通過意圖使用相機。代碼幾乎是從Android documentation here直接,但它不適合我。安卓相機意圖不會在RESULT_OK情況下返回
在對startActivityForResult()的調用後,相機應用程序按預期打開,但在拍攝照片後(?!)它永遠不會返回。具體而言,在我拍照並選擇接受圖標(Galaxy Nexus上的複選標記)後,它不會返回。但是在我選擇取消圖標(同一部手機上的「X」)後,它會返回。
下面的代碼(注意,我是從一個片段,而不是活動工作):
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_layout, container, false);
final Button btnCamera = (Button) view.findViewById(R.id.cameraid);
View.OnClickListener handler = new View.OnClickListener() {
public void onClick(View v) {
if (v == btnCamera) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// create a file to save the image
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "MyImages");
imagesFolder.mkdirs();
File image = new File(imagesFolder, "image_001.jpg");
Uri uriSavedImage = Uri.fromFile(image);
// start the image capture Intent
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
}
}
btnCamera.setOnClickListener(handler);
}
和
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(getActivity(), "Image saved to:\n" +
data.getData(), Toast.LENGTH_LONG).show();
}
else if (resultCode == Activity.RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
什麼我需要改變,以使這項工作?謝謝。
謝謝迪倫, - 會的。 – gcl1