2

I want to get image from camera, using

public void LicenseCameraIntent() {
    Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(cameraIntent, PROFILE_REQUEST_CAMERA);
}

Return bitmap, but it works perfectly. Unfortunately image show as a really small size.

So, i have to keep searching until i get

public void licenseCameraIntent() {
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    Uri mImageCaptureUri1 = Uri.fromFile(new File(Environment.getExternalStorageDirectory(),
            "tmp_avatar_" + String.valueOf(System.currentTimeMillis()) + ".jpg"));

    cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, mImageCaptureUri1);
    cameraIntent.putExtra("return-data", true);
    startActivityForResult(cameraIntent, LICENSE_REQUEST_CAMERA);
}

On OnActivityResult data always show null result. How I can fix this issue. It is possible using first solution but return bigger image?

Thanks

latifalbr
  • 153
  • 2
  • 11

2 Answers2

3

while you are capture image from the camera you have created a file for an image that is your image full path so make it globally.

 Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (cameraIntent.resolveActivity(getPackageManager()) != null) {
    // Create the File where the photo should go
    File photoFile = null;
    try {
        photoFile = createImageFile();
    } catch (IOException ex) {
        // Error occurred while creating the File
        Log.i(TAG, "IOException");
    }
    // Continue only if the File was successfully created
    if (photoFile != null) {
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photoFile));
        startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
    }
}

and the image file, mCurrentPhotoPath is the full path of image

  private File createImageFile() throws IOException {
    // Create an image file name
    String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
    String imageFileName = "JPEG_" + timeStamp + "_";
    File storageDir = Environment.getExternalStoragePublicDirectory(
            Environment.DIRECTORY_PICTURES);
    File image = File.createTempFile(
            imageFileName,  // prefix
            ".jpg",         // suffix
            storageDir      // directory
    );

    // Save a file: path for use with ACTION_VIEW intents
    mCurrentPhotoPath = "file:" + image.getAbsolutePath();
    return image;
}

after onActivityResult() you get the image from mCurrentPhotoPath

    @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
        try {
            mImageBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath));
            mImageView.setImageBitmap(mImageBitmap);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

For more info, you can check this link Capture Image from Camera and Display in Activity

Mohit Suthar
  • 8,725
  • 10
  • 37
  • 67
  • I got your idea. But still problem occurred when i tried to get image bitmap show FileNotFoundException.. Even though photopath already the same. Do you know why? – latifalbr Nov 08 '17 at 06:59
  • @sikuru13 Have you add permission for storage read and write? Are you using Activity or Fragment? – Mohit Suthar Nov 08 '17 at 07:51
  • i am using activity and already set permission in manifest @Mohit Suthar – latifalbr Nov 08 '17 at 08:24
2

try this,

public static int REQUEST_CAMERA = 111;

private void cameraIntent() {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(intent, REQUEST_CAMERA);
}

code for onStartActivityForResult

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == Activity.RESULT_OK) {
        if (requestCode == REQUEST_CAMERA) {
            _FileName = "image file";
            capturePic = null;
            file_pdf = null;

            Bundle extras = data.getExtras();
            Bitmap imageBitmap = (Bitmap) extras.get("data");
            //mImageView.setImageBitmap(imageBitmap);
            img_ticket_detail.setImageBitmap(imageBitmap);
            capturePic = imageBitmap;
        }

    }

}

also you can use glide library for show image

Upendra Shah
  • 2,218
  • 17
  • 27
Jinal Awaiya
  • 441
  • 3
  • 14