我是android的新手,我正在尝试使手机摄像头拍照并以图像视图显示。捕获图像后,将正确保存图像,但此后,应用程序将停止运行,此时应在图像视图中显示图像。任何帮助表示赞赏。我搜索了一些主题,但仍然无济于事。
这是代码:

package myfirstapp.myapps.me.camera;

import ...

public class MainActivity extends ActionBarActivity {
    ImageButton camBtn;
    ImageView imageView;
    ScrollView scrollView;

    private File imageFile;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void OpenCam(View view) {
        Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        imageFile=new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"image.jpg");
        Uri tempURI=Uri.fromFile(imageFile);
        intent.putExtra(MediaStore.EXTRA_OUTPUT, tempURI);
        intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);

        startActivityForResult(intent, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 0)//==0 the same where startActivityForResult(intent, 0) so we are in the same process
        {


            switch (resultCode){
                case Activity.RESULT_OK:
                    if(imageFile.exists())
                    {
                        Toast.makeText(MainActivity.this, "Image was saved at "+imageFile.getAbsolutePath(), Toast.LENGTH_SHORT)
                                .show();
                        Bitmap myBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath());

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

                        myImage.setImageBitmap(myBitmap);
                    }
                    else
                    {
                        Toast.makeText(MainActivity.this, "Image wasn't saved", Toast.LENGTH_SHORT)
                                .show();
                    }
                    break;
                case Activity.RESULT_CANCELED:
                    Toast.makeText(MainActivity.this, "Image capture was cancelled", Toast.LENGTH_SHORT)
                            .show();
                    break;
            }
        }
    }
}


堆栈跟踪


  由以下原因引起:java.lang.RuntimeException:无法传递结果
  ResultInfo {who = null,request = 0,result = -1,data = null}到活动
  {myfirstapp.myapps.me.camera/myfirstapp.myapps.me.camera.MainActivity}:
  java.lang.NullPointerException在
  android.app.ActivityThread.deliverResults(ActivityThread.java:3410)在
  android.app.ActivityThread.performResumeActivity(ActivityThread.java:2817)
  在
  android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2859)

最佳答案

请输入以下代码:

    public static final int REQUEST_IMAGE_CAPTURE = 1;
    public static final int RESULT_LOAD_IMAGE = 10;
     private Bitmap myBitmap;
     ImageView myImage ;
        public void OpenCam(View view) {

              Intent takePictureIntent = new             Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    if     (takePictureIntent.resolveActivity(getPackageManager()) != null)
                    {

                        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
                    }
    }


并且,

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data)
    {


        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK)
        {
            Bundle extras = data.getExtras();
            myBitmap= (Bitmap) extras.get("data");

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

                    myImage.setImageBitmap(myBitmap);
        }

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

            myBitmap= BitmapFactory.decodeFile(picturePath);

            //          photoUri = picturePath;



        myImage.setImageBitmap(myBitmap);

        }

    }

10-08 02:47