如何在从相机中拾取或捕获的imageview中显示图像

时间:2016-10-28 12:02:08

标签: android android-intent imageview

如何在从相机中拾取或捕获的imageview中显示图像 我正在尝试这个代码,但只有当我们从图库中选择它时才会起作用(从相机不显示)并且图像是旋转的形式

private void pickImage() {

    Intent pickIntent = new Intent();
    pickIntent.setAction(Intent.ACTION_GET_CONTENT);
    pickIntent.setType("image/*");

    Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    String manufacturer = Build.MANUFACTURER;
    if(!(manufacturer.contains("samsung")) && !(manufacturer.contains("sony")) && !(manufacturer.contains("lge"))) {
        String filename = System.currentTimeMillis() + ".jpg";
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.TITLE, filename);
        Uri cameraPicUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, cameraPicUri);
    }

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

    startActivityForResult(chooserIntent, GALLEY_REQUEST_CODE);
}

和onActivityResult()

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == GALLEY_REQUEST_CODE && resultCode == Activity.RESULT_OK) {

        try {
            InputStream inputStream = getContentResolver().openInputStream(data.getData());
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

            int height = (int) getResources().getDimension(R.dimen.location_image_width);
            int width = (int) getResources().getDimension(R.dimen.location_image_height);
            Bitmap smallImage = Bitmap.createScaledBitmap(bitmap, width, height, false);


            locationPhoto.setImageBitmap(smallImage);


        } catch (Exception e) {

        }
        if (data == null) {
            //Display an error
            Constant.showLog("data null ");
            return;
        }

        } 
    }

2 个答案:

答案 0 :(得分:1)

在您的代码中,您正在为相机和图库创建单独的意图。但是你只打算从画廊拍照。

使用相机在按钮水龙头上拍摄照片并在图像视图中显示的代码。你可以参考一下。

 Button photoButton = (Button) this.findViewById(R.id.button1);
            photoButton.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
                    startActivityForResult(cameraIntent, CAMERA_REQUEST); 
                }
            });
        }

        protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
            if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {  
                Bitmap photo = (Bitmap) data.getExtras().get("data"); 
                imageView.setImageBitmap(photo);
            }  
        } 

答案 1 :(得分:0)

此作品

我需要几个小时才能完成这项工作。该代码几乎是来自developer.android.com的复制粘贴,但略有不同。

AndroidManifest.xml:

上申请此权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

在您的活动中,首先定义:

static final int REQUEST_IMAGE_CAPTURE = 1;
private Bitmap mImageBitmap;
private String mCurrentPhotoPath;
private ImageView mImageView;

然后在onClick:

中触发此意图
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getPackageManager()) != null) {
    // Create the File where the photo should go
    File photoFile = null;
    try {
        photoFile = createImageFile();
    } catch (IOException ex) {
        // Error occurred while creating the File
        Log.i(TAG, "IOException");
    }
    // Continue only if the File was successfully created
    if (photoFile != null) {
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
        startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
    }
}

添加以下支持方法:

private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  // prefix
            ".jpg",         // suffix
            storageDir      // directory
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}

然后收到result:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        try {
            mImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath));
            mImageView.setImageBitmap(mImageBitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

使其有效的原因是MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath)),developer.android.com.的代码不同原始代码给了我一个FileNotFoundException.