2017-02-24 173 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

相關問題