I am trying to add a code in my current app so that on one button click ,camera should open and one snap should be taken.
here is my code :
Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
try {
PackageManager pm =this.getPackageManager();
final ResolveInfo mInfo = pm.resolveActivity(i, 0);
Intent intent = new Intent();
intent.setComponent(new ComponentName(mInfo.activityInfo.packageName, mInfo.activityInfo.name));
intent.setAction(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
startActivity(intent);
}
catch (Exception e){ Log.i(TAG, "Unable to launch camera: " + e); }
Error : it displays -> Complete action uing
Not able to figure it out what i am doin wrong, can any one help me out in this issue.
First
Add permission to your manifest
<uses-feature android:name="android.hardware.camera" android:required="fase" />
Second
static final int REQUEST_IMAGE_CAPTURE = 1;
#Override
protected void onCreate (Bundle savedInstanceState) {
//Your code
//dispatchTakePictureIntent() <-- call this on some button click etc.
}
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
Third
Retrieve your Snap
#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");
mImageView.setImageBitmap(imageBitmap);
}
}
Retrieved Image is only a small size thumbnail. To get/save full size Image
Related
I'm putting together a camera app using implicit intents for a college assignment, and I can't figure out what's going wrong with my Uri. I'm assigning a jpg file, then opening the camera using MediaStore. ACTION_IMAGE_CAPTURE as shown below. When I call selectImage(), I want the chosen viewing app to go straight to my recently taken photo. However, although the photo is saved and visible within the gallery, I'm getting an "Item not found" Toast, and I'm looking at an overview of all the image folders. Where am I going wrong?
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
photoFile = new File(Environment.getExternalStorageDirectory(),"cameraTest.jpg");
outputFile = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", photoFile);
Log.i("debugging",outputFile.getPath().toString());
}
public void capturePhoto(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFile);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, REQUEST_IMAGE_CAPTURE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Intent mediaScanIntent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
mediaScanIntent.setData(outputFile);
this.sendBroadcast(mediaScanIntent);
Log.i("debugging",outputFile.getPath().toString());
}
}
public void selectImage(View view) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
Log.i("debugging",outputFile.getPath().toString());
intent.setDataAndType(outputFile, "image/*");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
}
I have a dialogue, which asks you to choose if to take a picture or to upload one from gallery. The taken/chosen image I set as background on a Button. how can I handle both outputs, as I can't use 2 times onActivityResult?
Here is the method that invokes the camera:
private void invokeCamera() {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
}
And the method that lets you choose from a gallery:
private void openGallery() {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
I handle the image received from the camera on the following way:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && data != null) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = (Bitmap) extras.get("data");
if (photo != null) {
findViewById(), and whose background you want to update
if (Build.VERSION.SDK_INT > 16) {
imageUploader5.setBackground(new BitmapDrawable(getResources(), photo));
}
} else {
imageUploader5.setBackgroundDrawable(new BitmapDrawable(getResources(), photo));
}
}
}
}
}
First, make a global variable
private final static int GET_PHOTO_BITMAP = 1234;
Then do the following
private void invokeCamera() {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, GET_PHOTO_BITMAP);
}
private void openGallery() {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, GET_PHOTO_BITMAP);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == GET_PHOTO_BITMAP && data != null) {
Bundle extras = data.getExtras();
if (extras != null) {
Bitmap photo = (Bitmap) extras.get("data");
if (photo != null) {
findViewById(), and whose background you want to update
if (Build.VERSION.SDK_INT > 16) {
imageUploader5.setBackground(new BitmapDrawable(getResources(), photo));
}
} else {
imageUploader5.setBackgroundDrawable(new BitmapDrawable(getResources(), photo));
}
}
}
}
}
Here the key part is request code which is the second parameter of the startActivityForResult(Intent intent, int requestCode)
you can have different requestCodes for different operations and handle it this way in
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(requestCode) {
case code1:
//task1
break;
case code2:
//task2
break;
//and so on
}
}
but since your case both the callbakcs for startActivityForResult() are supposed to perform the same operation, you can pass same code for both the calls as I have done in the above solution. But make sure that you pass same code when the operations done in the callback are similar.
http://www.c-sharpcorner.com/UploadFile/9e8439/how-to-make-a-custom-camera-ion-android/I have made a custom camera app and after click a picture i want to preview it in an image view but when i am previewing it in image view,image is being scaled from actual size of picture.
his is how I made it work for me! Assuming you are capturing an image, and would like to show the captured image in a new Activity, you can follow this way:
First on the click of button you can:
public void cameraFuture(View view) // <-- onClick() method of Camera Button
{
Intent intent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File file = new File(Environment.getExternalStorageDirectory(),
"MyPhoto.jpg");
outPutfileUri = Uri.fromFile(file);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outPutfileUri);
startActivityForResult(intent, TAKE_PIC);
}
Then on the onActivityResult() method, you can do this way:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == TAKE_PIC && resultCode==RESULT_OK){
Toast.makeText(this, outPutfileUri.toString(),Toast.LENGTH_LONG).show();
Intent bitIntent = new Intent(this, CameraTake.class);
bitIntent.putExtra("imageUri", outPutfileUri);
startActivity(bitIntent);
finish();
}
}
And in the next Activity, you can receive the file this way:
Intent receiveIntent = getIntent();
uri = receiveIntent.getParcelableExtra("imageUri");
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
receiveImg= null;
} else {
receiveImg= extras.getString("PASSER");
}
} else {
receiveImg= (String) savedInstanceState.getSerializable("PASSER");
}
File imgFile = new File(Environment.getExternalStorageDirectory(),"MyPhoto.jpg");
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.camOut);
myImage.setImageBitmap(myBitmap);
}
Hope it helps!
This link completely described what you want. Here is the summary:
1- Add this to the Manifest:
< uses-feature android:name="android.hardware.camera" android:required="true" />
2- Add this line to your class:
static final int REQUEST_IMAGE_CAPTURE = 1;
3- call this method where you want to open camera:
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
}
}
4- Override onActivityResult:
#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 ivImage = (ImageView) findViewById(R.id.ivImage);
ivImage.setImageBitmap(imageBitmap);
}
}
I hope it help you.
I want to add remove option in image chooser intent, I successfully added gallery option and camera option and those are working fine but I want to add remove option, if I choose remove option the image should remove from imageview.
Thanks in advance :)
My Code:
public void edit_profile_pic(View view) {
Uri uri = null;
Intent GalleryIntent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
Intent CameraIntent =new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
CameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
Intent chooserIntent = Intent.createChooser(CameraIntent, "Choose");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {GalleryIntent, CameraIntent});
startActivityForResult(chooserIntent, RESULT_LOAD_IMG);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
ImageView imageView = (ImageView) findViewById(R.id.profile_profile_picture);
if (requestCode == REQUEST_CODE && resultCode == Activity.RESULT_OK)
{
Bitmap bitmap = null;
if(data.getData()!=null)
{
try
{
InputStream stream = getContentResolver().openInputStream(data.getData());
bitmap = BitmapFactory.decodeStream(stream);
stream.close();
imageView.setImageBitmap(bitmap);
}
catch (Exception e)
{
e.printStackTrace();
}
}
else
{
bitmap=(Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(bitmap);
}
super.onActivityResult(requestCode, resultCode, data);
}
}
I was able to do this with an intent chooser by creating a new activity with the theme NoDisplay like this:
First create the activity which will be used to notify the main activity that the user wants to delete the image:
public class DeleteProfileImageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent resultIntent = new Intent();
resultIntent.putExtra("remove_image", true);
setResult(Activity.RESULT_OK, resultIntent);
finish();
}
}
You can specify the icon and label in the manifest. Make sure you also set the theme to NoDisplay to avoid it covering up the actual activity with the intent chooser.
<activity
android:name=".activity.profilesetup.DeleteProfileImageActivity"
android:label="#string/title_activity_delete_profile_image"
android:icon="#drawable/ic_delete_image_red"
android:theme="#android:style/Theme.NoDisplay"
/>
Then you can add it to the intent chooser. I check if the image has been set previously to avoid adding the option when there is no image.
if (imageSet) {
Intent removeImageIntent = new Intent(activity, DeleteProfileImageActivity.class);
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {removeImageIntent} );
}
Then you can listen on activity result for intent data with the key specified in the DeleteProfileImageActivity, currently remove_image
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (data.hasExtra("remove_image") && imageSet) {
// remove/reset the image here
}
}
You have to create a alert Dailog. There you can give option, and where you can add remove option. On remove option use this:
else if (options[item].equals("Remove")) {
ImageView.setImageDrawable(null);
dialog.dismiss();
}
For more help you can refer http://www.theappguruz.com/blog/android-take-photo-camera-gallery-code-sample/
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");
}