【问题标题】:Android Camera Take Picture and SAVE or send to next activityAndroid 相机拍照并保存或发送到下一个活动
【发布时间】:2013-06-20 03:42:41
【问题描述】:

我是初学者,我需要帮助。如何用相机拍照并保存或发送到下一个活动?

我尝试了几个选项,即带有图片回调的拍照和有意图的表面视图/拍照。但是,两者都不能在 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
        }
    }
}

【问题讨论】:

  • “无法正常工作”信息量不大。
  • ICS 和 JB 它在 Gingerbread 上运行得很好,当它被摔倒或者不会打开并且没有保存或照片时。
  • 看到这个链接你得到了答案。 stackoverflow.com/questions/15115498/let-user-crop-image/…
  • 现在是 SurfaceCreated 上的 NPE-> mCamera.setPreviewDisplay

标签: android android-camera


【解决方案1】:

代替这一行: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");

【讨论】:

  • fos.write(data) 和 fos.write(data,0,data.length) 没有区别。 FileOutputStream 有两种写入字节数组的方法。
【解决方案2】:

我遇到了同样的问题,我之前只是解决了它。 问题是mPicture 是PictureCallback 的对象。您不能设置从StyloveFoto.this 指向Filter.class 的意图,因为它位于PictureCallback 接口中。试试这个:

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

java 中这么大的陷阱....希望它有所帮助:)

【讨论】:

    【解决方案3】:

    要么你像这样保存图片并将路径传递给另一个活动

    final File file = new File(Environment.getExternalStorageDirectory() + "/" + System.currentTimeMillis() + "_pic.jpg");
            OutputStream output = null;
            try {
                output = new FileOutputStream(file);
                output.write(data);
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                if (null != output) {
                    try {
                        output.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
    

    或 从相机活动中传递数据:

    Intent intent = new Intent(getApplicationContext(), your_class.class);
        intent.putExtra("path", path);
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        startActivity(intent);
    

    接收活动

    byte[] data = getIntent().getByteArrayExtra("path");
    Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
    image.setImageBitmap(scaleDownBitmapImage(bitmap, 300, 200));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多