我的工作產生的圖像效果的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