在应用程序启动时启动相机
Start camera on app startup
我想构建一个应用程序,它可以立即运行相机、拍照,然后对照片进行处理。
我使用这两种方法拍照并保存到设备:
static final int REQUEST_TAKE_PHOTO = 1;
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Toast.makeText(this, "Failed to save to picture", Toast.LENGTH_LONG).show();
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
String mCurrentPhotoPath;
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
这是我的 onCreate 方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
}
我想在应用程序启动时立即启动相机。
我该怎么做?
不要将您的方法放在 onCreate
中:如果 activity 从未被销毁,它可能不会被调用(事实上,它经常发生)。
这样就可以了。
@Override
protected void onResume() {
super.onResume();
dispatchTakePictureIntent();
}
更新:由于没有任何反应,我建议您按照以下步骤来调试问题:
记录方法的调用,以便了解它是否被调用:
private void dispatchTakePictureIntent() {
Log.d("CameraApp", "dispatchTakePictureIntent was called");
// ...
}
记录可能的问题,不忽略 "elses" 子句:
private void dispatchTakePictureIntent() {
// ...
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// ...
if (photoFile != null) {
// ...
} else {
Log.e("CameraApp", "photoFile was null");
}
} else {
Log.e("CameraApp", "No activity available to call the intent");
}
}
通过这种方式,你至少可以检测到没有任何反应的原因:
如果你永远看不到 "dispatchTakePictureIntent was called",onResume 方法有问题,因为 dispatchTakePictureIntent 实际上从未被调用
如果其他两个日志打印在 Logcat 中,那么您的意图和代码 在 您的方法中有问题.
我想构建一个应用程序,它可以立即运行相机、拍照,然后对照片进行处理。 我使用这两种方法拍照并保存到设备:
static final int REQUEST_TAKE_PHOTO = 1;
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Toast.makeText(this, "Failed to save to picture", Toast.LENGTH_LONG).show();
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
}
String mCurrentPhotoPath;
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
// Save a file: path for use with ACTION_VIEW intents
mCurrentPhotoPath = "file:" + image.getAbsolutePath();
return image;
}
这是我的 onCreate 方法:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
}
我想在应用程序启动时立即启动相机。 我该怎么做?
不要将您的方法放在 onCreate
中:如果 activity 从未被销毁,它可能不会被调用(事实上,它经常发生)。
这样就可以了。
@Override
protected void onResume() {
super.onResume();
dispatchTakePictureIntent();
}
更新:由于没有任何反应,我建议您按照以下步骤来调试问题:
记录方法的调用,以便了解它是否被调用:
private void dispatchTakePictureIntent() { Log.d("CameraApp", "dispatchTakePictureIntent was called"); // ... }
记录可能的问题,不忽略 "elses" 子句:
private void dispatchTakePictureIntent() { // ... if (takePictureIntent.resolveActivity(getPackageManager()) != null) { // ... if (photoFile != null) { // ... } else { Log.e("CameraApp", "photoFile was null"); } } else { Log.e("CameraApp", "No activity available to call the intent"); } }
通过这种方式,你至少可以检测到没有任何反应的原因:
如果你永远看不到 "dispatchTakePictureIntent was called",onResume 方法有问题,因为 dispatchTakePictureIntent 实际上从未被调用
如果其他两个日志打印在 Logcat 中,那么您的意图和代码 在 您的方法中有问题.