I am trying to capture image from camera. But it returns null Intent onActivityResult .
Here is my code
CaptureImageFromCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 7);
}
});
And onActivityResult is
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 7 && resultCode == RESULT_OK) {
try {
if(data.getData() == null) {
bitmap = (Bitmap)data.getExtras().get("data");
} else {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), data.getData());
}
ImageViewHolder.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
I am new to Android so please explain answer in detail.
Thanks in advance..!
Start Your work by passing intent through this :
startActivityForResult(new Intent(MediaStore.ACTION_IMAGE_CAPTURE), 100);
so after capturing the image from camera use to get data about images through intent :
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
Uri yourCapturedImage = intent.getData();
}
Make sure you have added camera permissions, write permissions in manifest and also cross verify run time permissions.
Use your Intent like below, and specify the path where captured image will be write -
intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
Uri outputUri = Uri.fromFile("path of your directory");
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputUri);
startActivityForResult(intent, 7);
You can also use setImageURI() in onActivityResult() instead of setImageBitmap()
First of all add the camera permissions in android manifest. If your android version is greater than lollipop then you need to add run time permissions See documentations here
<uses-permission android:name="android.permission.CAMERA"/>
and then your code will be
CaptureImageFromCamera.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 7);
}
});
and then use setImageUrI() in your onActivityResult()
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode)
{
case 7:
/*case 7 for image results and display image in imageview via Uri*/
if (resultCode == RESULT_OK)
{
Uri imageUri = data.getData();
ImageViewHolder.setImageURI(imageUri);
}
return;
default:
return;
}
}
Uri imageUri = data.getData() will no more work..
first get bitmap from data like this:
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
val imageBitmap = data.extras.get("data") as Bitmap
imageView.setImageBitmap(imageBitmap)
}
}
then save bitmap image.. please find details here:
https://developer.android.com/training/camera/photobasics
Related
The case is when user selects camera, he has the flexibility to either capture image or record video and the user shall be able to show the same in one's app.
For this case, MediaStore.INTENT_ACTION_VIDEO_CAMERA intent is used but no callback comes in either case of capturing image or recording video.
Intent intent = new Intent(MediaStore.INTENT_ACTION_VIDEO_CAMERA);
startActivityForResult(intent, VIDEO_CAMERA);
For getting result, code used as follows:-
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED) {
return;
}
if (resultCode == RESULT_OK) {
if (requestCode == VIDEO_CAMERA) {
Uri uri = data.getData();
}
}
}
To start the camera and get the result back in onActivityResult(), you should create an Intent with MediaStore.ACTION_IMAGE_CAPTURE:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Similarly, to capture a video, use MediaStore.ACTION_VIDEO_CAPTURE
I finally found a solution and it is as follows:-
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
Intent chooserIntent = Intent.createChooser(takePictureIntent, "Capture Image or Video");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[]{takeVideoIntent});
startActivityForResult(chooserIntent,VIDEO_CAMERA);
And finally receives the callback in onActivityResult and I got the uri in this way:-
#Override
public void onActivityResult(int requestCode, int resultCode, #Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == RESULT_CANCELED) {
return;
}
if (resultCode == RESULT_OK) {
if (requestCode == VIDEO_CAMERA) {
Uri uri;
if (data == null || data.getData() == null) {
Bitmap bitmap (Bitmap)data.getExtras().get("data");
// TODO:Get uri from bitmap for image
uri = getImageUri(context, bitmap);
} else {
//Get uri for video
uri = data.getData();
}
}
}
}
I am doing a module which needs to convert image into pdf. i have successfully implemented the camera and can display its image. but my problem is getting the uri of that image. i saw a code snippet here in StackOverflow and followed it but it returns null.
here is my sample code:
#Override
public void onClick(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 == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
uri = data.getData();
if(uri == null)
{
tvUri.setText("null");
}else{
tvUri.setText(uri.toString());
}
}
}
to test if it is null, i proceeded to set the textview into its value if it has one, but if not, then i set it to null.
protected void onActivityResult(int requestCode, int resultCode, Intent intent)
{
Uri u = intent.getData();
}
try this
uri = data.getExtras().get("data");
instead of
uri = data.getData();
I've got a code that starts intent to take picture from camera
Intent pictureActionIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(pictureActionIntent, PhotoUtility.CAMERA_PICTURE);
And after picture is taking back it should calls onActivityResult
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (resultCode != Activity.RESULT_OK)
return;
if (requestCode == PhotoUtility.CAMERA_PICTURE) {
File f = PhotoUtility.saveFromCamera(data, picIV);
imagePath = f.getAbsolutePath();
} else if (requestCode == PhotoUtility.GALLERY_PICTURE) {
File f = PhotoUtility.saveFromGallery(data, getActivity(), picIV);
imagePath = f.getAbsolutePath();
}
}
With simple Fragment it works fine, but if I use DialogFragment onActivityResult is not calling. How can solve this problem using DialogFragment?
I read an text input from the user, this input i use it as a name of a picture i'm taking by the camera.
i store this name and the path of the image name into Sqlite Database.
what I'm trying to do is, after clicking OK to accept the taken picture, i want the saved path to be displayed in a toast.
the problem is, when I click OK to accept the picture, nothing is being displayed and i cant switch from the camera activity to the activity that called the camera activity"the previous activity"
Note: I'm using the emulator not a real device.
OnClickListener btn_TakePictureListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
String imgPath = retrievePath();
intent.putExtra(MediaStore.EXTRA_OUTPUT, getImageUri(imgPath));
startActivityForResult(intent, RequestCode);
}
};
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RequestCode && resultCode == RESULT_OK) {
String s = data.getData().toString();
Toast.makeText(getBaseContext(), ""+s, Toast.LENGTH_SHORT).show();
}
}
if you are passing Uri for you image then you can retrieve image as taken by camera:
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
Environment.getExternalStorageDirectory(), "temp.jpg")));
startActivityForResult(intent, 1);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == NONE)
return;
if (requestCode == 1) {
// Set the file save path with directory
File picture = new File(Environment.getExternalStorageDirectory()
+ "/temp.jpg");
Uri imageuri= Uri.fromFile(picture);
//set to imageview here
}
}
EDIT:
For Getting data uri in onActivityResult start Camra Activiyt as:
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI,IMAGE_UNSPECIFIED);
startActivityForResult(intent, 2);
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == NONE)
return;
if (requestCode == 1) {
Uri uriimg = data.getData();
Toast.makeText(getBaseContext(), ""+uriimg.toString(), Toast.LENGTH_SHORT).show();
}
}
In my application, I'm trying to make it so the User can press a button, which will allow them to take a picture using the stock camera application on their phone.
I am following the guide to using an external Camera app to capture images that I can use in my own app from the Android Developers Guide (http://developer.android.com/guide/topics/media/camera.html#intent-receive)
I'm having trouble with the onActivityResult() method, it apparently takes in 3 parameters
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if(resultCode == RESULT_OK) {
Log.w("borre","Image saved to:\n" + data.getData());
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}
}
But at the moment, the data Intent is coming back as null, so calling any methods on the Intent parameter throws a NullPointerException
Here's the code I'm using to call up the Camera application (It's basically the same as the code in the guide)
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
Uri fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
Has anyone had this problem or knows why this Intent is coming back as null?
your are getting data part null bez you are not setting intent.setDataAndType() when you are starting Acitivty.like
public static final String IMAGE_UNSPECIFIED = "image/*";
Intent intent = new Intent(Intent.ACTION_PICK, null);
intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED);
startActivityForResult(intent, 3);
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == 0)
return;
if (requestCode == 2) {
Uri uri=data.getData(); //YOU GET DATA HERE
}
//OR
if (requestCode == 3) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = extras.getParcelable("data");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.JPEG, 75, stream);// (0 - 100)????
imageView.setImageBitmap(photo);
}
}
or in your case getting image path use:
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
//pic path
File picture = new File(Environment.getExternalStorageDirectory() + "/temp.jpg");
}