Android 2.2 SDK - PhotoCapture example not working
I'm integrating exact code from here: http://labs.makemachine.net/2010/03/sim开发者_运维知识库ple-android-photo-capture/
How the activity should work: click "button" -> go to default camera. Take photo. "Retake" button works, "cancel" button works (brings back to "button" layout), but the "ok" button doesn't work (and then the image should appear above the "button" in the previous layout). Does this have something to do with how it's saving image to SD card? I can't figure it out! Also, I'm testing this app on a device.
Got it to work:
package com.android.xxx;
import java.io.File;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Window;
public class CameraView extends MenusHolder {
protected String _path;
protected boolean _taken;
protected static final String PHOTO_TAKEN = "photo_taken";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.create_event_view);
File imageDirectory = new File(Environment.getExternalStorageDirectory() + "/MyFolder/");
imageDirectory.mkdirs();
_path = Environment.getExternalStorageDirectory() + "/MyFolder/temporary_holder.jpg";
startCameraActivity();
}
protected void startCameraActivity() {
File file = new File(_path);
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(resultCode) {
case 0:
finish();
break;
case -1:
onPhotoTaken();
break;
}
}
protected void onPhotoTaken() {
_taken = true;
finish();
Intent newView1 = new Intent(CameraView.this, CreateEventView.class);
CameraView.this.startActivity(newView1);
}
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putBoolean(CameraView.PHOTO_TAKEN, _taken);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
if (savedInstanceState.getBoolean(CameraView.PHOTO_TAKEN)) {
onPhotoTaken();
}
}
}
精彩评论