2013-06-23 51 views
1

我是初學者,我需要幫助。 如何用相機拍照並保存或發送到下一個活動? 請幫助Android相機拍照並保存或發送到下一個活動

我試過許多選項,(takepicture與圖片回調surfaceview,採取意圖) 既不會在Android 2.3.3

編輯 - 我的代碼正常工作:

@Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.sf_foto); 
     mCamera = getCameraInstance(); 
     mCameraPreview = new SF_CameraPreview(this, mCamera); 
     FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview); 
     preview.addView(mCameraPreview); 

     ImageButton captureButton = (ImageButton) findViewById(R.id.button_capture); 
     captureButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       mCamera.takePicture(myShutterCallback, mPicture_RAW, mPicture); 
      } 
     }); 
    } 

    private Camera getCameraInstance() { 
     Camera camera = null; 
     try { 
      camera = Camera.open(0); 
      camera.setDisplayOrientation(90); 
     } catch (Exception e) { 
      // cannot get camera or does not exist 
     } 
     return camera; 
    } 

    ShutterCallback myShutterCallback = new ShutterCallback(){ 
     @Override 
     public void onShutter() {} 
    }; 

    PictureCallback mPicture_RAW = new PictureCallback(){ 
     @Override 
     public void onPictureTaken(byte[] arg0, Camera arg1) {} 
    }; 

    PictureCallback mPicture = new PictureCallback() { 
     @Override 
     public void onPictureTaken(byte[] data, Camera camera) { 
      File pictureFile = getOutputMediaFile(); 
      if (pictureFile == null) { 
       return; 
      } 
      try { 
       FileOutputStream fos = new FileOutputStream(pictureFile); 
       fos.write(data); 
       fos.flush(); 
       fos.close(); 
      } catch (FileNotFoundException e) { 
       e.printStackTrace(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
      Intent i = new Intent(StyloveFoto.this, Filter.class); 
      startActivity(i); 
     } 
    }; 

    protected File getOutputMediaFile() { 
     File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),"KWAlbum"); 
     if (!mediaStorageDir.exists()) { 
      if (!mediaStorageDir.mkdirs()) { 
       Log.d("KWAlbum", "failed to create directory"); 
       return null; 
      } 
     } 
     // Create a media file name 
     File mediaFile = new File(mediaStorageDir.getPath() + File.separator + "KW" + ".jpg"); 

     return mediaFile; 
    } 

我表面觀:

public class SF_CameraPreview extends SurfaceView implements SurfaceHolder.Callback{ 

    private SurfaceHolder mSurfaceHolder; 
    private Camera mCamera; 

    public SF_CameraPreview(Context context, Camera camera) { 
     super(context); 
     this.mCamera = camera; 
     this.mSurfaceHolder = this.getHolder(); 
     this.mSurfaceHolder.addCallback(this); 
     this.mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
    } 

    @Override 
    public void surfaceCreated(SurfaceHolder surfaceHolder) { 
     try { 
      mCamera.setPreviewDisplay(surfaceHolder); 
      mCamera.startPreview(); 
     } catch (IOException e) { 
      // left blank for now 
     } 
    } 

    @Override 
    public void surfaceDestroyed(SurfaceHolder surfaceHolder) { 
     //mCamera.stopPreview(); 
     mCamera.release(); 
    } 

    @Override 
    public void surfaceChanged(SurfaceHolder surfaceHolder, int format, 
     int width, int height) { 
     // start preview with new settings 
     try { 
      mCamera.setPreviewDisplay(surfaceHolder); 
      Camera.Parameters parameters = mCamera.getParameters(); 
      parameters.set("orientation", "portrait"); 
      mCamera.setParameters(parameters); 
      mCamera.startPreview(); 
     } catch (Exception e) { 
      // intentionally left blank for a test 
     } 
    } 
} 
+1

「不正常」是不是很豐富。 – CommonsWare

+0

ICS和JB它在秋季拍攝或無法開啓並且沒有保存或照片時在薑餅上運行得很好。 – Ares

+0

看到這個鏈接,你得到了你的答案。 http://stackoverflow.com/questions/15115498/let-user-crop-image/15263571#15263571 –

回答

0

,而不是這一行:fos.write(data);

write : fos.write(data,0,data.length); 

,如果你想將它傳遞給下一個活動:

Intent i = new Intent(StyloveFoto.this, Filter.class); 
i.putExtra("myImage",data); 
startActivity(i); 

,然後在此類過濾器在onCreate方法

byte[] myImage = getIntent()getIntent().getByteArrayExtra("myImage"); 
+0

fos.write(data)和fos.write之間沒有區別(數據,0,data.length)。 FileOutputStream有兩種寫字節數組的方法。 – user1991679

0

我得到了同樣的問題,我只是解決它之前。 問題是mPicturePictureCallback的一個對象。您無法設置意圖從StyloveFoto.this指向Filter.class,因爲它位於PictureCallback界面中。試試這個:

Intent i = new Intent(getBaseContext() , Filter.class); 
startActivity(i); 

這樣Java中的大陷阱....希望它有助於:)

相關問題