2017-02-24 97 views
0

我正在制作一个应用程序,用户从设备相机中单击一张照片,然后在下一个活动中将此图像设置为图像视图。SD卡中的Android相机存储

当我在第一个活动中单击相机按钮时,会发生什么情况,设备相机的get会打开,然后用户将捕获按钮闭合,然后在下一个活动中将其设置在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”的某个特定文件夹中的存储卡中。 帮帮我吧。

+0

[android的拍照每隔5秒](http://stackoverflow.com/questions/23045291/taking-pictures-in-android-every-5-seconds) –

回答

0

如果你想捕捉图像没有用户intraction然后请参考这个示例代码哪一个是由谷歌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(); 
+0

u能PLS使可能的复制上述代码的变化..bcz我不能改变完整的代码 – Neha