android - 如何拍照显示在 `ImageView`中并保存图片?

标签 android android-camera android-imageview

我需要用相机拍一张照片,保存照片,在ImageView中显示,什么时候我单击 Imageview全屏 模式显示。

以后需要将图片发送到internet

这就是我所做的:

public void captureImage(View v) {
    Intent camera_intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(camera_intent, CAMERA_PIC_REQUEST);

}

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

    imgView = (ImageView) findViewById(R.id.formRegister_picture);
    imgView.setScaleType(ImageView.ScaleType.CENTER_CROP);

    super.onActivityResult(requestCode, resultCode, data);
    switch(requestCode){
        case CAMERA_PIC_REQUEST:
            if(resultCode==RESULT_OK){
                Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
                imgView.setImageBitmap(thumbnail);
            }
    }
}

最佳答案

您可以通过在代码中添加以下行来调用相机 Activity :

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);              
private static int RESULT_IMAGE_CLICK = 1;

                cameraImageUri = getOutputMediaFileUri(1);

                // set the image file name
                intent.putExtra(MediaStore.EXTRA_OUTPUT, cameraImageUri);
                startActivityForResult(intent, RESULT_IMAGE_CLICK);

现在创建文件 Uri 因为在一些 android 手机中你会在 return 中得到 null 数据

所以这是获取图像 URI 的方法:

 /** Create a file Uri for saving an image or video */
        private static Uri getOutputMediaFileUri(int type) {

            return Uri.fromFile(getOutputMediaFile(type));
        }

        /** Create a File for saving an image or video */
        private static File getOutputMediaFile(int type) {

            // Check that the SDCard is mounted
            File mediaStorageDir = new File(
        Environment.getExternalStorageDirectory(), Environment.DIRECTORY_PICTURES);

            // Create the storage directory(MyCameraVideo) if it does not exist
            if (!mediaStorageDir.exists()) {

                if (!mediaStorageDir.mkdirs()) {

                    Log.e("Item Attachment",
                            "Failed to create directory MyCameraVideo.");

                    return null;
                }
            }
java.util.Date date = new java.util.Date();
        String timeStamp = getTimeStamp();

        File mediaFile;

        if (type == 1) {

            // For unique video file name appending current timeStamp with file
            // name
            mediaFile = new File(mediaStorageDir.getPath() + File.separator +abc+ ".jpg");

        } else {
            return null;
        }

        return mediaFile;
    }

用于检索点击的图像:

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


    // Here you have the ImagePath which you can set to you image view
                    Log.e("Image Name", cameraImageUri.getPath());

         Bitmap myBitmap = BitmapFactory.decodeFile(cameraImageUri.getPath()); 

            yourImageView.setImageBitmap(myBitmap);



// For further image Upload i suppose your method for image upload is UploadImage
File imageFile = new File(cameraImageUri.getPath());
                uploadImage(imageFile);

                        }




            }
        }

关于android - 如何拍照显示在 `ImageView`中并保存图片?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31445156/

相关文章:

android - 我们可以降低 android 中相机闪光灯的亮度吗?

Android 相机 Intent 仅在平板电脑中失败

android - Camera.takePicture 抛出 RunTimeException

Android - 在 Picasso 或 Glide 中下载百分比

android - Windows下如何在Android NDK中构建ffmpeg

android - 进度对话框在低于和等于 API 级别 9 Android 时显示不佳

android - ListView 中的 ImageView 大小问题

android - 将隐藏值分配给 ListView 项

java - 从 SQLite 数据库添加和检索图像

Android:从 SD 卡显示图像