Example usage for android.provider MediaStore ACTION_IMAGE_CAPTURE

List of usage examples for android.provider MediaStore ACTION_IMAGE_CAPTURE

Introduction

In this page you can find the example usage for android.provider MediaStore ACTION_IMAGE_CAPTURE.

Prototype

String ACTION_IMAGE_CAPTURE

To view the source code for android.provider MediaStore ACTION_IMAGE_CAPTURE.

Click Source Link

Document

Standard Intent action that can be sent to have the camera application capture an image and return it.

Usage

From source file:eu.codeplumbers.cosi.activities.ExpenseDetailsActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_expense_details);

    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);/*from www .j  ava  2s. c om*/
    getSupportActionBar().setHomeButtonEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);

    expenseDate = (EditText) findViewById(R.id.expenseDate);
    expenseLabel = (EditText) findViewById(R.id.expenseLabel);
    expenseAmount = (EditText) findViewById(R.id.expenseAmount);
    expenseCategory = (EditText) findViewById(R.id.expenseCategory);
    expenseReceipt = (ImageButton) findViewById(R.id.expenseReceipt);
    expenseReceipt.setScaleType(ImageView.ScaleType.FIT_CENTER);

    expenseReceipt.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
            ((Activity) ExpenseDetailsActivity.this).startActivityForResult(intent,
                    Constants.REQUEST_RECEIPT_CAPTURE);
        }
    });

    long expenseId = getIntent().getLongExtra("expenseId", -1);

    if (expenseId != -1) {
        expense = Expense.load(Expense.class, expenseId);

        if (expense != null) {
            expenseDate.setText(expense.getDate());
            expenseLabel.setText(expense.getLabel());
            expenseAmount.setText(expense.getAmount() + "");
            expenseCategory.setText(expense.getCategory());

            currentReceipt = Receipt.getByExpense(expense);

            if (currentReceipt != null) {
                expenseReceipt.setImageBitmap(FileUtils.base64ToBitmap(currentReceipt.getBase64()));
            }

            getSupportActionBar().setSubtitle("Expense details");

        } else {
            Toast.makeText(this, "Something went terribly wrong... Can't load note information",
                    Toast.LENGTH_LONG).show();
        }
    } else {
        getSupportActionBar().setSubtitle(getString(R.string.lbl_expenses_new));
        expenseDate.setText(DateUtils.formatDate(new Date().getTime()));
    }
}

From source file:com.pdftron.pdf.utils.ViewerUtils.java

public static Map readImageIntent(Intent data, Context context, Uri outputFileUri)
        throws FileNotFoundException, Exception {
    final boolean isCamera;
    if (data == null || data.getData() == null) {
        isCamera = true;//from ww w .  j a va  2s  .  c o m
    } else {
        final String action = data.getAction();
        if (action == null) {
            isCamera = false;
        } else {
            isCamera = action.equals(MediaStore.ACTION_IMAGE_CAPTURE);
        }
    }

    Uri imageUri;
    if (isCamera) {
        imageUri = outputFileUri;
        AnalyticsHandlerAdapter.getInstance().sendEvent(AnalyticsHandlerAdapter.CATEGORY_FILEBROWSER,
                "Create new document from camera selected");
    } else {
        imageUri = data.getData();
        AnalyticsHandlerAdapter.getInstance().sendEvent(AnalyticsHandlerAdapter.CATEGORY_FILEBROWSER,
                "Create new document from local image file selected");
    }

    String filePath;
    if (isCamera) {
        filePath = imageUri.getPath();
    } else {
        filePath = Utils.getRealPathFromImageURI(context, imageUri);
        if (Utils.isNullOrEmpty(filePath)) {
            filePath = imageUri.getPath();
        }
    }

    // try to get bitmap
    Bitmap bitmap = Utils.getBitmapFromImageUri(context, imageUri, filePath);

    // if a file is selected, check if it is an image file
    if (!isCamera) {
        // if type is null
        if (context.getContentResolver().getType(imageUri) == null) {
            String extension = MimeTypeMap.getFileExtensionFromUrl(imageUri.getPath());
            final String[] extensions = { "jpeg", "jpg", "tiff", "tif", "gif", "png", "bmp" };

            // if file extension is not an image extension
            if (!Arrays.asList(extensions).contains(extension) && extension != null && !extension.equals("")) {
                throw new FileNotFoundException("file extension is not an image extension");
            }
            // if type is not an image
        } else if (!context.getContentResolver().getType(imageUri).startsWith("image/")) {
            throw new FileNotFoundException("type is not an image");
        }
    }

    //////////////////  Determine if image needs to be rotated   ///////////////////
    File imageFile = new File(imageUri.getPath());
    ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED);

    int imageRotation = 0;
    switch (orientation) {
    case ExifInterface.ORIENTATION_ROTATE_270:
        imageRotation = 270;
        break;
    case ExifInterface.ORIENTATION_ROTATE_180:
        imageRotation = 180;
        break;
    case ExifInterface.ORIENTATION_ROTATE_90:
        imageRotation = 90;
        break;
    }

    // in some devices (mainly Samsung), the EXIF is not saved with the image so look at the content
    // resolver as a second source of the image's rotation
    if (imageRotation == 0) {
        String[] orientationColumn = { MediaStore.Images.Media.ORIENTATION };
        Cursor cur = context.getContentResolver().query(imageUri, orientationColumn, null, null, null);
        orientation = -1;
        if (cur != null && cur.moveToFirst()) {
            orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0]));
        }

        if (orientation > 0) {
            imageRotation = orientation;
        }
    }

    if (imageRotation != 0) {
        Matrix matrix = new Matrix();
        matrix.postRotate(imageRotation);
        bitmap = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }

    Map<String, Object> output = new HashMap<>();
    output.put("bitmap", bitmap);
    output.put("uri", imageUri);
    output.put("path", filePath);
    output.put("camera", isCamera);
    return output;
}

From source file:com.ultramegasoft.flavordex2.util.PhotoUtils.java

/**
 * Get an Intent to capture a photo.//from w  ww .j ava 2s  .  co m
 *
 * @return Image capture Intent
 */
@Nullable
public static Intent getTakePhotoIntent(@NonNull Context context) {
    try {
        final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        final Uri uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".fileprovider",
                getOutputMediaFile());
        intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            intent.setClipData(ClipData.newUri(context.getContentResolver(), null, uri));
            intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
        } else {
            final List<ResolveInfo> activities = context.getPackageManager().queryIntentActivities(intent,
                    PackageManager.MATCH_DEFAULT_ONLY);
            for (ResolveInfo activity : activities) {
                final String name = activity.activityInfo.packageName;
                context.grantUriPermission(name, uri, Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
            }
        }

        return intent;
    } catch (IOException e) {
        Log.e(TAG, "Failed to create new file", e);
    }
    return null;
}

From source file:com.semfapp.adamdilger.semf.hazardIdF5.java

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    // Create the File where the photo should go
    File photoFile = null;//ww  w.  ja v  a  2  s.c om
    try {
        photoFile = data.createImageFile();
        imageFileCurrentPath = photoFile.getPath();
    } catch (Exception ex) {
        Toast.makeText(getActivity().getApplicationContext(), "Error: " + ex.toString(), Toast.LENGTH_LONG)
                .show();
        System.out.println(ex.toString());
    }
    // Continue only if the File was successfully created
    if (photoFile != null) {
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}

From source file:arun.com.chameleonskinforkwlp.activities.CameraCapturerActivity.java

private void launchCamera() {
    final Intent capturePicIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // Check if any app can handle this intent for us.
    final ComponentName componentName = capturePicIntent.resolveActivity(getPackageManager());
    if (componentName != null) {
        File imageFile = null;// w w w. j  a v a2  s.  c om
        try {
            imageFile = createTemporaryFile();
        } catch (IOException ex) {
            Timber.e("Error while creating image file", ex);
        }
        if (imageFile != null) {
            final Uri photoURI = FileProvider.getUriForFile(this, "arun.com.chameleonskinforkwlp.fileprovider",
                    imageFile);
            capturePicIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
            startActivityForResult(capturePicIntent, REQUEST_IMAGE_CAPTURE);
        } else {
            onCameraLaunchingFailed();
        }
    } else {
        onNoCameraDetected();
    }
}

From source file:io.hypertrack.sendeta.util.images.EasyImage.java

private static Intent createChooserIntent(Context context, String chooserTitle, boolean showGallery)
        throws IOException {
    Uri outputFileUri = createCameraPictureFile(context);
    List<Intent> cameraIntents = new ArrayList<>();
    Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    PackageManager packageManager = context.getPackageManager();
    List<ResolveInfo> camList = packageManager.queryIntentActivities(captureIntent, 0);
    for (ResolveInfo res : camList) {
        final String packageName = res.activityInfo.packageName;
        final Intent intent = new Intent(captureIntent);
        intent.setComponent(new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
        intent.setPackage(packageName);/*  w  w w  .  j  a  va 2  s  .  c  om*/
        intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
        cameraIntents.add(intent);
    }
    Intent galleryIntent;

    if (showGallery) {
        galleryIntent = createGalleryIntent();
    } else {
        galleryIntent = createDocumentsIntent();
    }

    Intent chooserIntent = Intent.createChooser(galleryIntent, chooserTitle);
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS,
            cameraIntents.toArray(new Parcelable[cameraIntents.size()]));

    return chooserIntent;
}

From source file:com.waz.zclient.pages.main.conversation.AssetIntentsManager.java

private void captureImage() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    pendingFileUri = getOutputMediaFileUri(IntentType.CAMERA);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, pendingFileUri);
    callback.openIntent(intent, IntentType.CAMERA);
}

From source file:com.example.michael.bumpy.EditDetailsActivity.java

public void imageButtonListener(View v) {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    imageButtonToUpdate = (ImageButton) v;

    if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }/*from   w  w w .j av  a 2  s .c  o  m*/
}

From source file:de.damdi.fitness.activity.create_exercise.ImageFragment.java

public void takePhoto(View view) {
    Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    File photo = new File(Environment.getExternalStorageDirectory(), "temp_pic.jpg");
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo));
    mTempImageUri = Uri.fromFile(photo);
    startActivityForResult(intent, CreateExerciseActivity.TAKE_PICTURE);
}

From source file:com.semfapp.adamdilger.semf.SiteInstructionF3.java

private void dispatchTakePictureIntent() {
    Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    // Create the File where the photo should go
    File photoFile = null;/*from w  w  w .  j  a va2s .c o  m*/
    try {
        photoFile = data.createImageFile();
        imageFileCurrentPath = photoFile.getPath();
    } catch (Exception ex) {
        Toast.makeText(getActivity(), "Error: " + ex.toString(), Toast.LENGTH_LONG).show();
        System.out.println(ex.toString());
    }
    // Continue only if the File was successfully created
    if (photoFile != null) {
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
        startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
    }
}