Set gallery image as button background - android

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?

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);

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

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

Related

Android Camera Height and Width [duplicate]

This question already has answers here:
Android Camera Intent: how to get full sized photo?
(8 answers)
Closed 5 years ago.
I am currently doing a fault reporting page whereby the end user is able to report an image either by capturing using the camera or selecting a photo from the gallery. I am not sure why but when the image is taken using the camera, the height and width returned is about 220 by 180 which is a very blur image. I have tried looking online for other tutorials but my code seems to be the same as the others. Not too sure if it is my phone or what.
My code for Camera Intent:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);
My code after Capturing image from Camera:
thumbnail = (Bitmap) data.getExtras().get("data");
bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
ivImage.setVisibility(View.VISIBLE);
ivImage.setImageBitmap(thumbnail);
I've tried getting the width and height before and after compression, both returns the same value.
May be you should not compress the image rather simply use BitmapFactory.decodeFile(filePath) or any suitable method for getting your image from file into a bitmap (if not done already) and then set the bitmap object to the ImageResource like this:
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
mImageView.setImageBitmap(bitmap);
And don't simply type cast the data received from intent. As far as I know about camera activity class, it will return a URI of the captured image. Try to receive it in onActivityResult() like this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode != RESULT_OK) return;
if (requestCode == REQUEST_CAMERA) {
Uri photoUri = data.getData();
// Calculate the bitmap here according to the width of the device
((ImageView) findViewById(R.id.captured_image)).setImageBitmap(bitmap);
}
super.onActivityResult(requestCode, resultCode, data);
}

Android Camera Photo Thumbnail Orientation

We have been using a bunch of code that uses the camera with the desired end result, but I want to get to the bottom of this with clean code. I'm simply following the Android docs here verbatim, and getting a rotated thumbnail. Below is the code, please find the working project in this branch of my Bitbucket repository.
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
ImageView view = (ImageView) findViewById(R.id.imageView);
view.setImageBitmap(imageBitmap);
}
}
I know that this is just the thumbnail. But it seems the thumbnail is completely useless, unless you get the full file and read the exif info from that.
I know that StackOverflow says "get the exif rotation from the full image file and rotate the actual bitmap before recompressing it into another jpg file". But isn't this a little too much unnecessary computation? What good is the thumbnail if it's useless by itself without getting the orientation from the full file?
Am I missing something?

Getting full sized picture from camera or gallery

I'm trying to get a picture from user during a registration proccess. So I have a form, and a button that starts camera, galerry, dropbox etc and asks user to choose or take a pic.
At the button onClick method I'm using the example at the 2nd answer of this topic Allow user to select camera or gallery for image and it opens a dialog with my image apps as options. After taking or choosing a foto, my code at onActivityResult is the following:
Bundle extras = data.getExtras();
Bitmap mImageBitmap = (Bitmap) extras.get("data");
foto.setImageBitmap(mImageBitmap)
So I have 2 problems. First, this get only a thumbnail image, and not the full-sized picture I need. Second, this only works if user chooses to take a new pic with camera...
for getting the fullsize pic, this code seems good Android Camera Intent: how to get full sized photo? but its only for camera-taken pics, and also with this chooser intent I'm not sure where this code would fit. Should I verify witch source use has choosed? If so, how could I do that?
EDIT: The chooser intent offers me 4 options of image sources: Camera, Gallery, Dropbox and ASTRO File Manager. Of course more options would be available if I had other apps installed. So I've checked the intent contents at each case:
CAMERA: act=inline-data (has extras)
DROPBOX and GALLERY: dat="file path"
ASTRO: dat="file path" (has extras)
so verifying if the intent has extras or not doesn't tells me the source. Also both
data.hasExtra("act") and
data.hasExtra("dat")
are returning false.
Here is the code for getting the full size image from camera captured image
declare object
private File dir, destImage,f;
private String cameraFile = null;
private static final int CAPTURE_FROM_CAMERA = 1;
in your activity
dir = new File(Environment.getExternalStorageDirectory()
.getAbsolutePath(), "MyApp");
if (!dir.isDirectory())
dir.mkdir();
destImage = new File(dir, new Date().getTime() + ".jpg");
cameraFile = destImage.getAbsolutePath();
try{
if(!destImage.createNewFile())
Log.e("check", "unable to create empty file");
}catch(IOException ex){
ex.printStackTrace();
}
f = new File(destImage.getAbsolutePath());
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
i.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(destImage));
startActivityForResult(i,CAPTURE_FROM_CAMERA);
in your onActivityResult
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case CAPTURE_FROM_CAMERA:
if (resultCode==RESULT_OK) {
if(f==null){
if(cameraFile!=null)
f = new File(cameraFile);
else
Log.e("check", "camera file object null line no 279");
}else
Log.e("check", f.getAbsolutePath());
Bitmap useBitmap = BitmapFactory.decodeFile(f.getAbsolutePath());
// now use this bitmap wherever you want
}
break;
}
}

pick image from gallery and convert into byte data how?

I need to pick an image from gallery and then convert it into byte data. I know how to pick image from gallery. Also I know how to convert image to byte data. But problem is i convert image that are in drawable but now I need to pick it from gallery and convert it to byte code. Any help
THanks
In onClick function I am using this code to pick image from gallery
Intent image = new Intent(Intent.ACTION_GET_CONTENT);
image.setType("Image/*");
startActivityForResult(image, 0);
And I have used following code to convert image that is in drawable to byte data.
bm = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
data = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 40 , data);
bitmapdata = data.toByteArray();
Now how would i convert image from gallery to byte data.
Thanks
In onActivityResult you will receive the Uri to your selected image like this:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == PICK_IMAGE && data != null && data.getData() != null){
Uri imageUri = data.getData();
//....
}
}
Then to retrieve it from the MediaStore you should use :
Bitmap bitmap =
MediaStore.Images.Media.getBitmap(this.getContentResolver(), imageUri);
after that, you should process the Bitmap like you do it now.

Image of imageview not remove?

I have an ImageView after getting id from xml.as
on ImageView we set clicklistener which open gallery and camera opetion you can set image from camera as well gallery
ain #2
profileimage = (ImageView) findViewById(R.id.profileimage);
profileimage.setBackgroundResource(R.drawable.no_img);
protected void onActivityResult(int requestCode, int resultCode,
Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
if (resultCode == RESULT_CANCELED) {
// TODO
return;
}
Log.e("request code", "1:" + requestCode);
switch (requestCode) {
case CAMERA_SELECT:
Log.e("in camera select", "1");
// Get the camera data
cameracalling(intent);
break;
case Gallery_Select:
ongallerycalling(intent,resultCode);
}
}
private void cameracalling(Intent intent){
Bitmap photo = (Bitmap) intent.getExtras().get("data");
profileimage.setImageBitmap(photo);
}
profileimage.buildDrawingCache();
Bitmap bmap = profileimage.getDrawingCache();
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bmap.compress(Bitmap.CompressFormat.JPEG, 90, bao);
byte [] ba = bao.toByteArray();
bitmapString=Base64.encodeBytes(ba);
Drawable draw = LoadImageFromWebOperations("" + objUserInformationSitesList.getProfileImage());
profileimage.setBackgroundDrawable(draw);
We are sending bitmap string to server image upload on server properly but when we open next time this screen that webservice call on which we upload image which will give all data (actually this user profile screen) .when we set server image then default image also set on background
objUserInformationSitesList this object which contains all information after parsing the web service. behind profile image ,default image also looking which set by me on number #1
if I unable to explain properly then please tell me.
use in onclicklistener
profileimage.setBackgroundResource(0);
You should replace below line:
profileimage.setBackgroundResource(R.drawable.no_img);
with this line:
profileimage.setImageResource(R.drawable.no_img);
You set the image as the background of drawable and later you set bitmap as image source. So imageview background doesn't change. You should set image as image resource.

Categories

Resources