1

I have a button in my code to take a picture:

<Button
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="@drawable/cameralogo"

            android:id="@+id/buttonCamera" />

When i click it it opens the camera and saves a picture, path is String mCurrentPhotoPath;

after the camera intent was displayed i want the button to show the image as background (android:background="mCurrent.....")???

how do do this?

Pedro Oliveira
  • 20,442
  • 8
  • 55
  • 82
Jonas
  • 1,356
  • 2
  • 11
  • 16

3 Answers3

1

Here is the solution.

You cannot set background only by path or URI, you'll need to create a Bitmap( and use ImageButton) or a Drawable out of it.

Using Bitmap and ImageButton:

Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
yourImageButton.setImageBitmap(bitmap);

Using Drawable and Button:

Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
Drawable d = new BitmapDrawable(getResources(),bitmap);
yourButton.setBackground(d);
Nativ
  • 3,092
  • 6
  • 38
  • 69
  • Yep you need to use it in an activity or use Context instance: context.getContentResolver(). By the way, if you find my solution helpful than please rate it :-) – Nativ Oct 21 '14 at 09:21
0

Have you had a look at this question yet? How to set the button background image through code

You cant do this in the xml but only programmatically. Just get a reference to the newly created picture like described here: How to get path of a captured image in android

Community
  • 1
  • 1
D4ngle
  • 21
  • 7
0

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 presenthere

Harshal
  • 126
  • 7