[英]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.