简体   繁体   English

如何在我的应用程序中将图像从相机意图添加到ImageView

[英]How to add image from camera intent to ImageView in my app

I am trying to take an image in my app and then return that image to a imageview. 我正在尝试在应用程序中拍摄图像,然后将该图像返回到imageview。 It takes the image but doesn't return the image to my ImageView 它需要图像,但不会将图像返回到我的ImageView

Here is what I have done: 这是我所做的:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    View rootView = inflater.inflate(R.layout.fragment_images, container, false);

    imagev = (ImageView) rootView.findViewById(R.id.MyImages);
    imagev.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            timeStamp = new SimpleDateFormat("ddMMMyyyy_HH:mm:ss").format(new Date());
            File imagesFolder = new File(Environment.getExternalStorageDirectory(), "My Taken Images");
            imagesFolder.mkdirs();
            image = new File(imagesFolder.getPath(), "Image_" + timeStamp + ".jpg");
            fileUri = Uri.fromFile(image);

            imageIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
            startActivityForResult(imageIntent, TAKE_PICTURE);
        }
    });
    return rootView;
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == TAKE_PICTURE && resultCode == RESULT_OK) {
            imageview = ThumbnailUtils.extractThumbnail(BitmapFactory.decodeFile(String.valueOf(fileUri), 512, 384);
            ExifInterface exif = null;
            try {
                exif = new ExifInterface(filep);
            } catch (IOException e) {
                e.printStackTrace();
            }
            int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, -1);
            if (orientation != -1) {
                switch (orientation) {
                    case ExifInterface.ORIENTATION_ROTATE_90:
                        rotate = 90;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_180:
                        rotate = 180;
                        break;
                    case ExifInterface.ORIENTATION_ROTATE_270:
                        rotate = 270;
                        break;
                    default:
                        rotate = 0;
                        break;
                }
            }
            Matrix matrix = new Matrix();
            matrix.postRotate(rotate);
            imagev.setImageBitmap(imageview);

        }
    }
}
}

What am I doing wrong here? 我在这里做错了什么? Please help! 请帮忙!

Thanks 谢谢

You have at least three two problems here. 您在这里至少有 三个 两个问题。

The reason you are not getting your result is that you are passing an invalid value to decodeFile() . 无法获得结果的原因是您将无效的值传递给了decodeFile() decodeFile() takes a path; decodeFile()采用路径; you are passing in a file:/// string. 您正在传递file:///字符串。 Either hold onto image from onCreateView() , or use getPath() on the Uri , to get the path to the file. onCreateView()保留image ,或在Uri上使用getPath()获取文件的路径。

Also: 也:

  • You go through a lot of trouble to set up a Matrix , then never appear to use it. 您在建立Matrix时遇到了很多麻烦,然后再也没有使用它。

  • Starting an activity in onCreateView() is bizarre. onCreateView()启动活动很奇怪。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM