[英]Android Camera storage in SD Card
我正在制作一个应用程序,其中用户单击设备照相机中的照片,然后在下一个活动中将此图像设置为图像视图。
发生的情况是,当我在第一个活动中单击“相机”按钮时,设备相机的获取打开,然后用户单击“捕获”按钮,然后在下一个活动中将其设置在imageview中。 这是我的代码部分
public void loadImagefromGallery(View view) {
try {
// photo = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() + "/DCIM", photoid + ".jpg"); //image gets stored in DCIM folder in device's inernal memory
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
imageUri = Uri.fromFile(photo);
startActivityForResult(intent, TAKE_PICTURE);
}
catch(Exception e)
{
e.printStackTrace();
}
这是我的onActivityResult方法
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case TAKE_PICTURE:
if (resultCode == Activity.RESULT_OK) {
try {
Intent intent = new Intent(this, websters.photobooth.ImageDisplay.class); //sending this image to next activity
intent.putExtra("picture", photoid + ".jpg");
startActivity(intent);
} catch (Exception e) {
Toast.makeText(this, e + "Failed to load", Toast.LENGTH_SHORT)
.show();
Log.e("Camera", e.toString());
}
}}
}
现在我想要的是单击“拍照”按钮时,然后打开“相机”,然后在3秒钟后自动单击图像,而无需任何用户交互。 同样,现在所有图像都存储在内部存储器中,我想将它们存储在存储卡中名为“ photobooth”的某个特定文件夹中。 帮帮我。
如果您想在不吸引用户的情况下捕获图像,请参考此示例代码,该示例代码由google https://github.com/googlesamples/android-Camera2Basic提供,此处简单易用CountDownTimer类可能为您提供了一些代码参考可以捕捉照片。
private void openCamera(int width, int height) {
if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestCameraPermission();
return;
}
setUpCameraOutputs(width, height);
configureTransform(width, height);
Activity activity = getActivity();
CameraManager manager = (CameraManager) activity.getSystemService(Context.CAMERA_SERVICE);
try {
if (!mCameraOpenCloseLock.tryAcquire(2500, TimeUnit.MILLISECONDS)) {
throw new RuntimeException("Time out waiting to lock camera opening.");
}
manager.openCamera(mCameraId, mStateCallback, mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
} catch (InterruptedException e) {
throw new RuntimeException("Interrupted while trying to lock camera opening.", e);
}
}
private void takePicture() {
lockFocus();
}
/**
* Lock the focus as the first step for a still image capture.
*/
private void lockFocus() {
try {
// This is how to tell the camera to lock focus.
mPreviewRequestBuilder.set(CaptureRequest.CONTROL_AF_TRIGGER,
CameraMetadata.CONTROL_AF_TRIGGER_START);
// Tell #mCaptureCallback to wait for the lock.
mState = STATE_WAITING_LOCK;
mCaptureSession.capture(mPreviewRequestBuilder.build(), mCaptureCallback,
mBackgroundHandler);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
new CountDownTimer(30000, 1000) {
public void onTick(long millisUntilFinished) {
openCamera(your_required_width, your_required_height)
}
public void onFinish() {
takePicture()
}
}.start();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.