我的工作产生的图像效果的Android应用程序。我希望我的应用的用户有两个选项。相机未returing回我捕捉到的图像
1)他们可以从图库中选择图片
2)他们可以从相机拍摄新照片
这里是我如何完成上述两项任务:
Button takePhotoFromCameraButton;
Button chooseFromGalleryButton;
String imagePath = Environment.getExternalStorageDirectory().getAbsolutePath() + System.currentTimeMillis() + "_image.jpg";
File imageFile = new File(imagePath);
imageUri = Uri.fromFile(imageFile);
在两个按钮,我传递相同的onClickListner。
@Override
public void onClick(View clickedView) {
int clickedViewId = clickedView.getId();
switch(clickedViewId) {
case R.id.takeFromCamera:
Intent imageCaptureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
imageCaptureIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(imageCaptureIntent,0);
break;
case R.id.chooseFromGallery:
Intent choosePictureIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(choosePictureIntent, 1);
break;
default:
// As we have only two buttons, and nothing else can be clicked except the buttons. So no need to put
// code in the "DEFAULT CASE"
}
}
而且我从两个捕获的结果,通过以下方式:
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch(requestCode) {
case 0:
Intent cameraIntent = new Intent(MainOptionsActivity.this,ApplyEffectsActivity.class);
cameraIntent.putExtra("imageFileUri", imageUri);
startActivity(cameraIntent);
break;
case 1:
Uri imageUriForGallery = intent.getData();
Intent galleryIntent = new Intent(MainOptionsActivity.this,ApplyEffectsActivity.class);
galleryIntent.putExtra("imageFileUri", imageUriForGallery);
startActivity(galleryIntent);
break;
}
}
}
的按钮画廊图像正常工作。但是当我通过按下第二个按钮调用摄像头并捕捉图像时,什么都不会发生。它停留在那里直到我取消相机并回到我的应用程序。我没有收到任何图像! 我哪里错了?不要介意这个愚蠢的问题,我只是一个Android的初学者! :(
没有帮助......我用一个onActivityResult方法为两种观点,而你写的链接,不这样做。 – 2013-03-23 17:01:33
会发生什么? onActivityResult是否永远不会调用,或者调用了错误的值? – Jacopofar 2013-03-23 17:32:43
你为什么要在onActivityResult中开始活动..?如果你想接收图像,你需要做的位图照片=(位图)data.getExtras()。get(“data”);像这样.. – Pragnani 2013-03-23 17:37:56