How to get image from camera - android

I have tried many codes from stackoverflow and internet.But I am able to find how to pick image from camera.I have used the following code,but data.getData() always returns null.Dont know how to solve.Pleas help this.
Intent takePictureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, PICK_IMAGE_CAMERA1);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == 30 && resultCode == Activity.RESULT_OK&&data!null)
{
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
imageView.setImageBitmap(imageBitmap);
}
}

Try this code #Thrishool,
private static final int CAMERA = 1;
//choosing the image from camera
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA);
//now get the data from the onActivity result
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
imageview.setImageBitmap(thumbnail);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.PNG,100, baos);
byte [] b=baos.toByteArray();
String temp = Base64.encodeToString(b, Base64.DEFAULT);
Log.e("savedImage",temp);
Toast.makeText(ProfileActivity.this, "Image Saved!", Toast.LENGTH_SHORT).show();
}
}
Try this and let me know #Thrishool

Related

How can I send a jpg picture from android app to a matlab server?

I have an application with one simple button which allows to take a picture. I want to send this picture to a matlab server. Here is my code:
button = (Button) findViewById(R.id.uploadButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//Toast.makeText(MainActivity.this,"Try Toast",Toast.LENGTH_LONG).show();
//Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
//startActivity(intent);
Intent cameraIntent = new Intent (MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
And this the others method:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
//did the user choose ok? If so, the code inside these curly braces will execute.
if (resultCode == RESULT_OK){
if (requestCode == CAMERA_REQUEST){
//we are hearing back from the camera
Bitmap cameraImage = (Bitmap) data.getExtras().get("data");
//at this point we have the image from the camera.
//imgSpecimenPhoto.setImageBitmap(cameraImage);
Bitmap bJPGcompress = codec(cameraImage, Bitmap.CompressFormat.JPEG, 100);
imgSpecimenPhoto.setImageBitmap(bJPGcompress);
}
}
}
private static Bitmap codec(Bitmap img, Bitmap.CompressFormat format,
int quality) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
img.compress(format, quality, baos);
byte[] array = baos.toByteArray();
return BitmapFactory.decodeByteArray(array, 0, array.length);
}

Convert the data received by camera into a bitmap in android

What i am having: i am using a camera to capture a image and i want to display it in a listview by passing bitmap to adapter in android
private void startCameraCapture() {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getActivity().getPackageManager()) != null) {
startActivityForResult(cameraIntent, 1024);
}
}
public void onActivityResult(int requestCode, int resultCode, Intent myData) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == 1024) {
.............. mycode
}
}
What i want to do :: i want to convert the data i received as myData into a bitmap in android
Convert byte[] to bitmap
Bitmap bmp = myData.getExtras().get("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();

How to Capture Image in Fragment Android

I am working with capturing an image and then show it on my ImageView in my fragment. I searched this through web and still I cannot make it.
Below is what I am doing.
btnCaptureImg.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 1888);
}
});
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
Log.e("LOG", ""+requestCode);
if (requestCode == 1888 && resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
super.onActivityResult(requestCode, resultCode, data);
}
The Logcat says 1888 only but the imageView did not load the image.
I also tried this
btnCaptureImg.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
File folder = new File(Environment.getExternalStorageDirectory().toString()+"/ImagesFolder/");
folder.mkdirs();
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
resultingFile = new File(folder.toString() + "/image.jpg");
Uri uriSavedImage=Uri.fromFile(resultingFile);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
startActivityForResult(cameraIntent, 1888);
}
});
But it throws exception:
E/AndroidRuntime(4824): java.lang.RuntimeException: Unable to resume activity {com.myapp.android/com.myapp.android.MyHomeActivity}: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=264032, result=-1, data=null} to activity {com.myapp.android/com.myapp.android.MyHomeActivity}: java.lang.NullPointerException
E/AndroidRuntime(4824): at com.myapp.android.MyFragment.onActivityResult(MyFragment.java:300)
Try putting
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, your_image_uri);
before this
startActivityForResult(cameraIntent, 1888);
I'm sorry mate but I can't seem to understand why it's giving that exception. Try this approach, it works for me.
Define the file globally and inside onActivityResult
if (requestCode == 1888 && resultCode == Activity.RESULT_OK) {
Bitmap photo = Media.getBitmap(getActivity().getContentResolver(), Uri.fromFile(resultingFile));
//Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
Check if your onActicityResult is beeing called. If not check:
onActivityResult is not being called in Fragment
First, in the fragment starts the intent to capture the image:
private void startIntentImageCapture() {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
try {
File storageDir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_PICTURES);
File image = File.createTempFile("imageName.jpg", ".jpg", storageDir);
path = Uri.fromFile(image);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, path);
getActivity().startActivityForResult(cameraIntent, CAPTURE_IMAGE_REQUEST_CODE);
} catch (IOException e) {
e.printStackTrace();
}
}
path variable, must be an public static Uri instance. And another public static Integer for the request code: public static final Integer CAPTURE_IMAGE_REQUEST_CODE = 100;
Then in the Activity, read this path and decode the image:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == YourFragment.CAPTURE_IMAGE_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
Bitmap imageBitmap = decodeFile(YourFragment.path);
//do something with your photo
}
}
}
I'm sure this is not the best and cleaner method but it works for me. Hope it helps!
EDIT
I forgot the code to decode the image.
public Bitmap decodeFile(Uri uriFile) throws OutOfMemoryError {
String filePath = uriFile.getPath();
BitmapFactory.Options bmOptions;
Bitmap imageBitmap;
try {
imageBitmap = BitmapFactory.decodeFile(filePath);
} catch (OutOfMemoryError e) {
bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 4;
bmOptions.inPurgeable = true;
imageBitmap = BitmapFactory.decodeFile(filePath, bmOptions);
}
return imageBitmap;
}
The bmOptions ensure that OutOfMemoryError doesn't occurs.

onActivityResult is not calling after taking camera picture

I am doing an android app, in which I have a functionality that, I need to open camera and take a picture. After taking picture onActivityResult is not called. my screen remains only in camera state.
My code:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File mediaFile = new File(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI.getPath());
Uri imageUri = Uri.fromFile(mediaFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, 1);
onActivityResult code
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (data != null && !data.toString().equalsIgnoreCase("Intent { }"))
switch (requestCode) {
case 1:
Log.i("StartUpActivity", "Photo Captured");
Uri uri = data.getData();
String imgPath = getRealPathFromURI(uri);
bitmap = (Bitmap) data.getExtras().get("data");
MediaStore.Images.Media.insertImage(getContentResolver(),
bitmap, null, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
img1.setImageBitmap(bitmap);
}
}
onActivityResult is probably called, but the statements inside your if statement and switch/case statements are not being executed, because they are never reached.
The code below includes two methods takePhoto and onActivityResult, is very short and works perfectly. Take a look at it, it probably will help you!
public void takePhoto(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1) {
photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
You can run your application in debug mode, after putting a breakpoint in onActivityResult method,
if it's stopping there after getting from the Camera then it's being called successfully.
and you could just change the if statement you're using making your code like this:
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data != null && resultCode = RESULT_OK)
switch (requestCode) {
case 1:
Log.i("StartUpActivity", "Photo Captured");
Uri uri = data.getData();
String imgPath = getRealPathFromURI(uri);
bitmap = (Bitmap) data.getExtras().get("data");
MediaStore.Images.Media.insertImage(getContentResolver(),
bitmap, null, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
img1.setImageBitmap(bitmap);
}
}
and note that it is more recommended to using final ints as the request code instead of rewriting them everytime they're needed
like :
private final int START_CAMERA_REQUEST_CODE = 1;
I think this may be help you
Follow Below Code step by step and compare with your code to avoid null exception
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 1);
1 OnActivity Result
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == Activity.RESULT_OK) {
if (requestCode == 1)
onCaptureImageResult(data);
}
}
2
onCaptureImageResult
public void onCaptureImageResult(Intent data) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.JPEG, 90, bytes);
Uri tempUri = getImageUri(getApplicationContext(), thumbnail); // call getImageUri
File finalFile = new File(getRealPathFromURI(tempUri));
file_path = finalFile.getPath().toString(); // your file path
imgview.setImageBitmap(thumbnail);
// this code will get your capture image bitmap}
3 getImageUri
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null); // your capture image URL code
return Uri.parse(path);
}
Using this code your screen will not remain more on camera state it will finish and image will be load as well you get image URL.
Bitmap mBitmap = BitmapFactory.decodeFile(imgPath);
pass Image path which you create from URI.
Probably, your activity is reloaded after camera is finished, but your code is not prepared to such situation.
See Android startCamera gives me null Intent and ... does it destroy my global variable?.
I think onActivity is called but it doesn't go into the if condition
Check only for null in if condition:
if (data != null )
switch (requestCode) {
case 1:
Log.i("StartUpActivity", "Photo Captured");
Uri uri = data.getData();
String imgPath = getRealPathFromURI(uri);
bitmap = (Bitmap) data.getExtras().get("data");
MediaStore.Images.Media.insertImage(getContentResolver(),
bitmap, null, null);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
img1.setImageBitmap(bitmap);
}
From where you are calling startActivityForResult(intent, 1),
and where you Overriding onActivityResult() ?
I mean if you call startActivityForResult() from Frgment then you should override onActivityResult() in fragment it self, same applies to Activity also.

Android camera intent losing reference of Imageview in Lollipop

I have an activity in which i am calling intent of camera on click, but when I capture image and get back to the activity result my Image view reference is null. I am getting URI correctly. This happens in android Lollipop.
Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(captureIntent, 1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_OK) {
if (requestCode == 1) {
Bundle extras = data.getExtras();
Bitmap thePic = extras.getParcelable("data");
//retrieve a reference to the ImageView
Log.i("dataa",String.valueOf(thePic));
Log.i("dataaimg",String.valueOf(image));
Log.i("dataaimgno",String.valueOf(imageno));
image.setImageBitmap(thePic); // here is the problem image reference is null when get back here
}
}
This code is working fine in versions below lollipop.
Try this:
private static final int CAPTURE_IMAGE_ACTIVITY_REQ = 0;
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(i, CAPTURE_IMAGE_ACTIVITY_REQ );
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQ) {
if (resultCode == RESULT_OK) {
Bitmap bp = (Bitmap) data.getExtras().get("data");
image = setImageBitmap(bp);
}
}

Categories

Resources