如何显示用户刚用相机拍摄的图像

时间:2015-01-14 09:45:10

标签: android image camera

我试图让用户选择从手机存储中选择图像,或者使用手机的相机应用程序选择一个图像,然后在imageView中显示该图像。在thisthis问题的帮助下,我可以在从存储中选择图像时显示图像。但是,如果我选择用相机拍照,虽然我没有收到错误,但图像不会显示在imageView中。

选择图片的功能如下:

public void choosePic(View view) {
    Intent pickIntent = new Intent();
    pickIntent.setType("image/*");
    pickIntent.setAction(Intent.ACTION_GET_CONTENT);

    Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    String pickTitle = "Select or take a new Picture"; // Or get from strings.xml
    Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);
    chooserIntent.putExtra
    (
      Intent.EXTRA_INITIAL_INTENTS, 
      new Intent[] { takePhotoIntent }
    );

    startActivityForResult(chooserIntent, SELECT_PICTURE);
}

之后由

处理
    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(requestCode == PICK_IMAGE && data != null && data.getData() != null) {
        Uri _uri = data.getData();

        //User had pick an image.
        Cursor cursor = getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
        cursor.moveToFirst();

        image = (ImageView) findViewById(R.id.imageView);
        image.setImageURI(_uri);
        cursor.close();
    }
    super.onActivityResult(requestCode, resultCode, data);
}

修改

借助@Murtaza Hussain的帮助,我找到了一个可行的解决方案:

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    Log.i("requestCode",""+requestCode);
    if(requestCode == PICK_IMAGE && resultCode==RESULT_OK && data != null && data.getData() != null) {
        Uri _uri = data.getData();

        //User had pick an image.
        Cursor cursor = getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
        cursor.moveToFirst();

        image = (ImageView) findViewById(R.id.imageView);
        image.setImageURI(_uri);
        cursor.close();
    }
    else if(requestCode== PICK_IMAGE && resultCode==RESULT_OK && data.getData() == null){

        Bundle extras = data.getExtras();
        Bitmap thePic = extras.getParcelable("data");


        image =(ImageView) findViewById(R.id.imageView);
        image.setImageBitmap(thePic);

    }

    super.onActivityResult(requestCode, resultCode, data);
}

2 个答案:

答案 0 :(得分:0)

您需要在onActivityResult()

中为其添加代码
if(requestCode==0 && resultCode==RESULT_OK ){

    Bundle extras = data.getExtras();
    Bitmap thePic = extras.getParcelable("data");


    image =(ImageView) findViewById(R.id.imageView);
    image.setImageBitmap(thePic);

}

答案 1 :(得分:0)

在您的活动中何时从相机捕获图像。写下面的代码。

Intent intent    = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),"/imageLarge"+System.currentTimeMillis()+".jpg"));
                intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri);

                try {
                    intent.putExtra("return-data", true);
                    startActivityForResult(intent, 111);
                } catch (ActivityNotFoundException e) {
                    e.printStackTrace();
                }

在onActivityResult()中编写以下代码..

   @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            switch (requestCode) {
            case 111:           

                if(resultCode == RESULT_OK)
                {
                    Log.v("","After camera capture path is --> "+mImageCaptureUri.toString().substring(8));         
                    Bitmap bitmap=BitmapFactory.decodeFile(mImageCaptureUri.toString().substring(8));
image.setImageBitmap(bitmap);

                }
                else
                {

                }
    }
    }

不要忘记添加一个权限。

 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

如有任何疑问,请告知我们。感谢。