您好我开始在Android中使用相机,就像它可以从here获得一样简单。
问题是在我拍摄照片后,由于某种原因,文件的uri只是空的。
步骤:
我跑了takePhoto()
func,手机的标准相机应用启动,
我拍了照片,我保存了。
我的应用程序运行onActivityResult(...)
func,因为它从摄像机返回控件,文件的uri只是空。
代码:
在我的活动中:
/**
* Intent code for capturing a photo
*/
private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 100;
/**
*
* Takes a photo
*/
public void takePhoto() {
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = CameraHelper.getOutputMediaFileUri(CameraHelper.MEDIA_TYPE_IMAGE); // create a file to save the image
Log.i("PHOTO", "file uri is: " + fileUri.toString());
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}
/**
*
* After taking the photo...
*/
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
if (data == null) {
Toast.makeText(this, "Camera error: data is null!", Toast.LENGTH_LONG).show();
Log.i("PHOTO", "Camera error: data is null!");
// Error is here, data does not contains the uri.
}
else {
Toast.makeText(this, "Image saved to:\n" + data.getData(), Toast.LENGTH_LONG).show();
Log.i("PHOTO", "Image saved to:\n" + data.getData());
}
}
else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
}
else {
// Image capture failed, advise user
}
}
}
CameraHelper类:
public class CameraHelper {
public static final int MEDIA_TYPE_IMAGE = 1;
/** Create a file Uri for saving an image or video */
public static Uri getOutputMediaFileUri(int type) {
return Uri.fromFile(getOutputMediaFile(type));
}
/** 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_PICTURES), "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()) {
Log.i("PHOTO", "directory is not exists yet!");
if (!mediaStorageDir.mkdirs()) {
Log.d("MyCameraApp", "failed to create directory");
return null;
}
else {
Log.i("PHOTO", "just made directory: " + mediaStorageDir.getAbsolutePath());
}
}
else {
Log.i("PHOTO", "directory is already exist: " + mediaStorageDir.getAbsolutePath());
}
// 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 {
return null;
}
return mediaFile;
}
}
日志输出:
01-13 16:15:37.485: I/PHOTO(6818): directory is already exist: /storage/sdcard0/Pictures/MyCameraApp
01-13 16:15:37.495: I/PHOTO(6818): file uri is: file:///storage/sdcard0/Pictures/MyCameraApp/IMG_20150113_161537.jpg
01-13 16:16:13.482: I/PHOTO(6818): Camera error: data is null!
所有图片都在文件夹中:Pictures/MyCameraApp
但onActivityResult(...)
不包含数据字段中的uri。
我做错了什么?