我正嘗試創建一個具有兩個ImageButtons的活動的應用程序。點擊每個按鈕時,相機將打開並拍攝照片。一旦圖片被保存,它就會在圖片按鈕中顯示爲預覽圖。我可以爲其中的一個做,但當點擊第二個時,它會覆蓋第一個圖像。多個ImageButton上的Android預覽圖片
有沒有辦法將單擊的元素傳遞給onActivityResult方法以設置哪個ImageButton應該包含圖片?
的代碼如下:
// Activity request codes
private static final int CAMERA_CAPTURE_IMAGE_REQUEST_CODE = 100;
private static final int CAMERA_CAPTURE_VIDEO_REQUEST_CODE = 200;
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
// directory name to store captured images and videos
private static final String IMAGE_DIRECTORY_NAME = "Hello Camera";
private Uri fileUri; // file url to store image/video
private ImageButton imageButton;
private ImageButton imageButton2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ask_activity_layout);
imageButton =(ImageButton) findViewById(R.id.imageButton);
imageButton2 =(ImageButton) findViewById(R.id.imageButton2);
// Checking camera availability
if (!isDeviceSupportCamera()) {
Toast.makeText(getApplicationContext(),
"Sorry! Your device doesn't support camera",
Toast.LENGTH_LONG).show();
// will close the app if the device does't have camera
finish();
}
}
啓動捕獲圖像的方法:
public void captureImage(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// start the image capture Intent
startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}
的onActivityResult方法:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// if the result is capturing Image
if (requestCode == CAMERA_CAPTURE_IMAGE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// successfully captured the image
// display it in image view
// data.getComponent();
previewCapturedImage(null);
} else if (resultCode == RESULT_CANCELED) {
// user cancelled Image capture
Toast.makeText(getApplicationContext(),
"User cancelled image capture", Toast.LENGTH_SHORT)
.show();
} else {
// failed to capture image
Toast.makeText(getApplicationContext(),
"Sorry! Failed to capture image", Toast.LENGTH_SHORT)
.show();
}
} else if (requestCode == CAMERA_CAPTURE_VIDEO_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// video successfully recorded
// preview the recorded video
previewCapturedImage(null);
} else if (resultCode == RESULT_CANCELED) {
// user cancelled recording
Toast.makeText(getApplicationContext(),
"User cancelled video recording", Toast.LENGTH_SHORT)
.show();
} else {
// failed to record video
Toast.makeText(getApplicationContext(),
"Sorry! Failed to record video", Toast.LENGTH_SHORT)
.show();
}
}
}
最後previewCapturedImage方法:
private void previewCapturedImage(View view) {
try {
// hide video preview
//videoPreview.setVisibility(View.GONE);
imageButton.setVisibility(View.VISIBLE);
// bimatp factory
BitmapFactory.Options options = new BitmapFactory.Options();
// downsizing image as it throws OutOfMemory Exception for larger
// images
options.inSampleSize = 8;
final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),
options);
**imageButton.setImageBitmap(bitmap);**
} catch (NullPointerException e) {
e.printStackTrace();
}
}
我想從onActivityResult點擊的實際ImageButton傳遞到previewCaptureImage方法來動態設置ImageButton或ImageButton2中的圖像。
任何與此有關的幫助將不勝感激。
感謝
勒布
PS:這裏發佈的代碼是取自下面的例子:http://www.androidhive.info/2013/09/android-working-with-camera-api/
它的工作原理!感謝你的回答。非常感激! – tzuvy