应用程序在生成位图时崩溃

时间:2015-11-07 12:30:17

标签: android string android-intent android-studio bitmap

所以我写了这个应该用意图拍照然后绘制全屏位图的应用程序,但是当我拍照应用说请等待然后崩溃。这是我的代码:

  ImageView iv;
  private static final int REQUEST_CODE_PHOTO = 101;
  private String path;

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    iv = (ImageView) findViewById(R.id.imageView);

    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);
    File output = new File(dir,"pic.png");
    cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT,Uri.fromFile(output));
    path = output.getAbsolutePath();
    startActivityForResult(cameraIntent, REQUEST_CODE_PHOTO);
}

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE_PHOTO) {
            Bitmap bitmap = BitmapFactory.decodeFile(path);
            iv.setImageBitmap(bitmap);
        }
    }

有人可以帮助我并说明问题所在。提前谢谢。

1 个答案:

答案 0 :(得分:3)

您的主要问题是忘记了setContentView(....);

中的onCreate(....)
setContentView(R.layout.yourlayout);

因此你的应用程序崩溃了

 iv = (ImageView) findViewById(R.id.imageView);

因为iv==null