【问题标题】:Take picture in android lollipop programmatically以编程方式在android棒棒糖中拍照
【发布时间】:2015-08-22 10:16:56
【问题描述】:

我正在开发一个应用程序,只需按下按钮即可拍照(前置摄像头)。该应用程序在 Android API 级别 10 和 15 上运行良好,但是当我在 Android Lollipop 中尝试时,我得到了强制关闭错误。

这是我的代码:

private void takePicture() {
    openCamera();
    //System.gc();
    camera.startPreview();
    new Handler().postDelayed(new Runnable() {

        @Override
        public void run() {
            camera.takePicture(
                    new ShutterCallback() {

                        @Override
                        public void onShutter() {
                            // TODO Do something when the shutter closes.
                        }
                    }, new PictureCallback() {

                        @Override
                        public void onPictureTaken(byte[] _data, Camera _camera) {

                        }
                    }, new PictureCallback() {

                        @Override
                        public void onPictureTaken(byte[] _data, Camera _camera) {
                            // TODO Do something with the image JPEG data.
                            Bitmap bitmap = BitmapFactory.decodeByteArray(_data, 0, _data.length);
                            imgPicture.setImageBitmap(bitmap);
                            Toast.makeText(TakePhotoActivity.this, "Length = " + _data.length, Toast.LENGTH_SHORT).show();
                            ByteArrayOutputStream bytes = new ByteArrayOutputStream();
                            bitmap.compress(Bitmap.CompressFormat.JPEG, 40, bytes);
                            File f = new File(Environment.getExternalStorageDirectory()
                                    + File.separator + "test.jpg");

                            // remember close de FileOutput
                            try {
                                f.createNewFile();
                                FileOutputStream fo = new FileOutputStream(f);
                                fo = new FileOutputStream(f);
                                fo.write(bytes.toByteArray());
                                fo.close();
                            }
                            catch (IOException e) {
                                // TODO Auto-generated catch block
                                e.printStackTrace();
                            }
                            CloseCamera();
                        }
                    });
        }
    }, 1000);
}

更新: 我找到了这个,但我仍然不知道该怎么做: http://developer.android.com/reference/android/hardware/Camera.PictureCallback.html

【问题讨论】:

  • 您的 LogCat 错误在哪里?你是说它崩溃了,但你没有显示 LogCat ......没有意义:)
  • 我无法在调试模式下测试我的应用程序,因为 Eclipse 无法识别带有 android lollipop 的手机。我只能使用另一个运行良好的 API 10 的 Android 设备。
  • 你想用什么手机?
  • 我用的是三星 Galaxy Tab A。
  • 尝试下载this driver

标签: android android-camera android-5.0-lollipop


【解决方案1】:

在 Lolipop 及以上情况发生了变化,你的迷恋可能是一个安全异常,因为你需要获得用户的许可才能使用他的相机或访问他的画廊,这样做here 是谷歌文档,用如何实现它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-26
    • 1970-01-01
    • 2015-08-12
    • 2015-03-31
    相关资源
    最近更新 更多