这几天困扰我的人都知道了。问题是,我不知道自己是否朝着正确的方向前进。
我的开始活动是ImageDisplay类,它为CustomCamera活动调用startActivityForRestult。拍摄照片后,用户将返回到ImageDisplay并显示捕获图像,但不会显示。我做错了什么?
第二件事,我猜它真的很重要,那时相机应该被释放?
ImageDisplay:
public class ImageDisplay extends Activity{
ImageView imageHolder;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.al_imagedisplay);
imageHolder = (ImageView) findViewById(R.id.imageView1);
Intent intent = new Intent(this, CustomCamera.class);
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0 && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("result");
imageHolder.setImageBitmap(photo);
}
}
}
CustomCamera:
public class CustomCamera extends Activity {
private Camera mCamera;
private NewItemSurfaceView mPreview;
Button captureButton;
public static final int MEDIA_TYPE_IMAGE = 1;
public static final int MEDIA_TYPE_VIDEO = 2;
public static final String MY_CAMERRA_APP = "MyCameraApp";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.al_newitem_camera);
captureButton = (Button) findViewById(R.id.buttonClick);
// Create an instance of Camera
mCamera = getCameraInstance();
mCamera.setDisplayOrientation(90);
// Create our Preview view and set it as the content of our activity.
mPreview = new NewItemSurfaceView(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.flCamera);
preview.addView(mPreview);
// Add a listener to the Capture button
captureButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// get an image from the camera
mCamera.takePicture(null, null, mPicture);
}
});
}
/** A safe way to get an instance of the Camera object. */
public static Camera getCameraInstance() {
Camera c = null;
try {
c = Camera.open(); // attempt to get a Camera instance
} catch (Exception e) {
// Camera is not available (in use or does not exist)
}
return c; // returns null if camera is unavailable
}
private PictureCallback mPicture = new PictureCallback() {
@Override
public void onPictureTaken(byte[] data, Camera camera) {
File pictureFile = getOutputMediaFile(MEDIA_TYPE_IMAGE);
if (pictureFile == null) {
Log.d("Camera error",
"Error creating media file, check storage permissions: ");
return;
}
Log.i("Picture", pictureFile.toString());
try {
FileOutputStream fos = new FileOutputStream(pictureFile);
fos.write(data);
fos.close();
} catch (FileNotFoundException e) {
Log.d("Camera error", "File not found: " + e.getMessage());
} catch (IOException e) {
Log.d("Camera error", "Error accessing file: " + e.getMessage());
}
Intent returnIntent = new Intent();
returnIntent.putExtra("result", pictureFile);
setResult(RESULT_OK, returnIntent);
finish();
}
};
/** Create a File for saving an image or video */
private static File getOutputMediaFile(int type) {
// To be safe, you should check that the SDCard is mounted
// using Environment.getExternalStorageState() before doing this.
File mediaStorageDir = new File(
Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),
"MyCameraApp");
// This location works best if you want the created images to be shared
// between applications and persist after your app has been uninstalled.
// Create the storage directory if it does not exist
if (!mediaStorageDir.exists()) {
if (!mediaStorageDir.mkdirs()) {
Log.i("MyCameraApp", "failed to create directory");
return null;
}
}
// Create a media file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
.format(new Date());
File mediaFile;
if (type == MEDIA_TYPE_IMAGE) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "IMG_" + timeStamp + ".jpg");
} else if (type == MEDIA_TYPE_VIDEO) {
mediaFile = new File(mediaStorageDir.getPath() + File.separator
+ "VID_" + timeStamp + ".mp4");
} else {
return null;
}
return mediaFile;
}
}
答案 0 :(得分:1)
像这样修改onActivityResult方法
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 0 && resultCode == RESULT_OK) {
String photoPath = (String) data.getExtras().get("result");
imageHolder.setImageBitmap(BitmapFactory.decodeFile("photoPath"));
}
}
在你的onPictureTaken方法中进行一些修改
Intent returnIntent = getIntent();
returnIntent.putExtra("result", pictureFile.getAbsolutePath());
setResult(RESULT_OK, returnIntent);
finish();