0

How to display images in imageview picked or captured from camera I am trying this code but it only work if we pick it from gallery(from camera is not displaying) and image is in rotated form

private void pickImage() {

    Intent pickIntent = new Intent();
    pickIntent.setAction(Intent.ACTION_GET_CONTENT);
    pickIntent.setType("image/*");

    Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    String manufacturer = Build.MANUFACTURER;
    if(!(manufacturer.contains("samsung")) && !(manufacturer.contains("sony")) && !(manufacturer.contains("lge"))) {
        String filename = System.currentTimeMillis() + ".jpg";
        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.TITLE, filename);
        Uri cameraPicUri = getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
        takePhotoIntent.putExtra(MediaStore.EXTRA_OUTPUT, cameraPicUri);
    }

    String pickTitle = "Select or take a new Picture"; 
    Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);
    chooserIntent.putExtra
            (
                    Intent.EXTRA_INITIAL_INTENTS,
                    new Intent[]{takePhotoIntent}
            );

    startActivityForResult(chooserIntent, GALLEY_REQUEST_CODE);
}

And onActivityResult()

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == GALLEY_REQUEST_CODE && resultCode == Activity.RESULT_OK) {

        try {
            InputStream inputStream = getContentResolver().openInputStream(data.getData());
            Bitmap bitmap = BitmapFactory.decodeStream(inputStream);

            int height = (int) getResources().getDimension(R.dimen.location_image_width);
            int width = (int) getResources().getDimension(R.dimen.location_image_height);
            Bitmap smallImage = Bitmap.createScaledBitmap(bitmap, width, height, false);


            locationPhoto.setImageBitmap(smallImage);


        } catch (Exception e) {

        }
        if (data == null) {
            //Display an error
            Constant.showLog("data null ");
            return;
        }

        } 
    }
Hanzala
  • 1,965
  • 1
  • 16
  • 43

2 Answers2

1

In your code you are creating separate intent for camera and gallery. But your calling only the intent for take photo from gallery.

Code for take picture and display in image view on a button tap using camera. You can refer it.

 Button photoButton = (Button) this.findViewById(R.id.button1);
            photoButton.setOnClickListener(new View.OnClickListener() {

                @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);
            }  
        } 
EKN
  • 1,886
  • 1
  • 16
  • 29
  • As per your code you are not calling startActivityForResult() for camera intent. – EKN Oct 28 '16 at 12:12
  • see again I am using `Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle); chooserIntent.putExtra ( Intent.EXTRA_INITIAL_INTENTS, new Intent[]{takePhotoIntent} );` – Hanzala Oct 28 '16 at 12:15
0

This works

It took me some hours to get this working. The code it's almost a copy-paste from developer.android.com, with a minor difference.

Request this permission on the AndroidManifest.xml:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

On your Activity, start by defining this:

static final int REQUEST_IMAGE_CAPTURE = 1;
private Bitmap mImageBitmap;
private String mCurrentPhotoPath;
private ImageView mImageView;

Then fire this Intent in an onClick:

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);
    }
}

Add the following support method:

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;
}

Then receive the result:

@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();
        }
    }
}

What made it work is the MediaStore.Images.Media.getBitmap(this.getContentResolver(), Uri.parse(mCurrentPhotoPath)), which is different from the code from developer.android.com. The original code gave me a FileNotFoundException.

Hanzala
  • 1,965
  • 1
  • 16
  • 43