Take an image and set is as the background in andr

2019-08-22 02:05发布

I was wondering if it were possible to take an image using the camera and then use that image in the background of my application. I am able to take and preview the image in my app, but don't know how to pass that data to the class that would set it as a background image. Is there a dynamic way to get this photo and set it as the background in my app?

1条回答
叛逆
2楼-- · 2019-08-22 02:49

To start the camera intent:

... 
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
activity.startActivityForResult(takePictureIntent, PHOTO_ACTIVITY_REQUEST_CODE);
...

Where PHOTO_ACTIVITY_REQUEST_CODE is just a integer constant unique within activity to be used as request codes while starting intent for results.

To Receive photo in the onActivityResult, and update background of the view

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == PHOTO_ACTIVITY_REQUEST_CODE && data != null) {
        Bundle extras = data.getExtras();
        if (extras != null) {
            Bitmap photo = (Bitmap) extras.get("data");
            if (photo != null) {
                // mView should refer to view whose reference is obtained in onCreate() using findViewById(), and whose background you want to update
                mView.setBackground(new BitmapDrawable(getResources(), photo));
            }
        }
    }
}

The above code does not use full size photo. For that, you will have to ask Photo intent to save it to a file, and read the file. Details are present here

查看更多
登录 后发表回答