【问题标题】:Android: error occurs when taking a picture in landscapeAndroid:横向拍照时出错
【发布时间】:2013-07-29 09:10:32
【问题描述】:

我正在尝试使用内置相机应用程序拍摄照片并通过 ImageView 进行查看。

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_photo);

    addButtonListeners();
    startCamera();
}

private void startCamera() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, PHOTO_TAKEN);
}

    protected void onActivityResult(int requestCode, int resultCode,
        Intent intent) {
    if (requestCode == PHOTO_TAKEN) {

        Bundle extras = intent.getExtras();
        photo = (Bitmap) extras.get("data");

        if (photo != null) {
            ImageView image = (ImageView) findViewById(R.id.image_background);
            image.setImageBitmap(photo);
        } else {
            Toast.makeText(this, R.string.unable_to_read_photo, Toast.LENGTH_LONG)
                    .show();
        }
    }
}

当将手机放在纵向位置时,此代码可以正常工作,但是当我以横向方式拍摄照片时它会中断,有什么想法为什么或如何解决这个问题?

【问题讨论】:

  • 断了是什么意思?您在 logcat 控制台中遇到什么错误?
  • 欢迎使用“独立于平台的 Java 和 Android”,我敢打赌,您的代码可以在 XXX 设备上运行,而在 +1 上则不行。告诉多少次:音频和视频应该在本机模式下处理,并且取决于设备,不管文档怎么说?!
  • 嗨,什么是坏的,它是怎么坏的?请粘贴一些LogCat 输出..
  • 尝试在清单文件中添加配置更改:键盘或者只是在配置更改方法上调用一个函数......

标签: android camera photo landscape


【解决方案1】:

问题没有定义足够的细节来确定回答,但我的猜测与 Shani Goriwal 相同。

配置更改事件似乎有问题 - 每次更改方向(从横向到纵向)时都会发生这种情况。

尝试将以下行添加到您的应用的 AndroidManifest: android:configChanges="orientation|screenSize"

(更多详情:http://developer.android.com/guide/topics/resources/runtime-changes.html

【讨论】:

    【解决方案2】:

    我找到了解释如何正确使用内置相机的教程。这是link

    我在 android 上相对较新,但从我所读到的是,每次显示器旋转时,android 都会创建某种新实例。所以你必须保存旋转的实例,这是通过以下代码完成的:

    /**
    * Here we store the file url as it will be null after returning from camera
    * app
    */
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
    
    // save file url in bundle as it will be null on scren orientation
    // changes
    outState.putParcelable("file_uri", fileUri);
    }
    
    /*
    * Here we restore the fileUri again
    */
    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        super.onRestoreInstanceState(savedInstanceState);
    
        // get the file url
        fileUri = savedInstanceState.getParcelable("file_uri");
    }
    

    如果您单击该链接,您应该转到第 11 号项目符号。在拍照后避免 NullPointerException。这里真正的英雄是 Ravi Tamada,他在使用相机方面做了出色的教程。我建议阅读整个教程。

    再次,我是新来的,所以如果对我在这里写的内容有任何更正,请更正。

    【讨论】:

      猜你喜欢
      • 2012-06-15
      • 2012-05-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-22
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多