0
private static final int CAMERA_REQUEST = 1337;
private void showCamera() {
    Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    cameraIntent.putExtra("category", "camera");
    startActivityForResult(cameraIntent, CAMERA_REQUEST);
}

I used this code to pick an image from the camera. and this is my activity result

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if (requestCode == PICK_IMAGE_REQUEST ) {
        filePath = data.getData();
        try {
            Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), filePath);
            imageView.setImageBitmap(bitmap);
            Toast.makeText(this, data.getDataString(), Toast.LENGTH_SHORT).show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    else if (requestCode == CAMERA_REQUEST) {
        filePath = data.getData();
            Log.i("hello", "REQUEST cALL");
            try {
                Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), filePath);
                imageView.setImageBitmap(bitmap);

            } catch (Exception e) {
                Log.i("hello", "Exception" + e.getMessage());
            }
    }

the camera is fine. and I can capture it

but why imageview cant pick the photos from camera?

but if I am picking from storage the imageview can change the image. can you see the false code?

Usman Maqbool
  • 3,351
  • 10
  • 31
  • 48

3 Answers3

0
data.getData();

This one doesnt give you a filepath of the captured image

You can follow this one

Getting path of captured image in Android using camera intent

protected void onActivityResult(int requestCode, int resultCode, Intent data) {  

        Bitmap photo = (Bitmap) data.getExtras().get("data"); 

       //get the URI of the bitmap from camera result
        Uri uri = getImageUri(getApplicationContext(), photo);


       // convert the URI to its real file path.
        String filePath = getRealPathFromURI(uri);

}

This method gets the URI of the bitmap that you can use to convert it to a file path

public Uri getImageUri(Context inContext, Bitmap inImage) {
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    inImage.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
    String path = Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
    return Uri.parse(path);
}

this method converst a URI to a file path

public String getRealPathFromURI(Uri uri) {
    String path = "";
    if (getContentResolver() != null) {
        Cursor cursor = getContentResolver().query(uri, null, null, null, null);
        if (cursor != null) {
            cursor.moveToFirst();
            int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
            path = cursor.getString(idx);
            cursor.close();
        }
    }
    return path;
}
Lorence Hernandez
  • 1,189
  • 12
  • 23
0

Try this, Hope it will help you.

 if (requestCode == CAMERA_REQUEST) {   
        Bitmap bitmap= (Bitmap) data.getExtras().get("data");
        imageView.setImageBitmap(bitmap);  
  }  
Jakir Hossain
  • 3,830
  • 1
  • 15
  • 29
0

To use image file path you have to do like below

1) Write a method to create an image file

String currentPhotoPath;
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 = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
    imageFileName,  /* prefix */
    ".jpg",         /* suffix */
    storageDir      /* directory */
);

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

2) Call camera intent

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

if (takePictureIntent.resolveActivity(getPackageManager()) != null) {

    File photoFile = null;
    try {
        photoFile = createImageFile();
    } catch (IOException ex) {
        // Error occurred while creating the File

    }
    // Continue only if the File was successfully created
    if (photoFile != null) {
        Uri photoURI = FileProvider.getUriForFile(this,
                                              "com.example.android.fileprovider",
                                              photoFile);
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI);
        startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
    }
  }
}

3) Now, you need to configure the FileProvider. In your app's manifest, add a provider to your application:

<application>
 ...
 <provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="${applicationId}.fileprovider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths"></meta-data>
 </provider>
 ...
</application>

4) create a resourse file res/xml/file_paths.xml

<?xml version="1.0" encoding="utf-8"?>
  <paths xmlns:android="http://schemas.android.com/apk/res/android">
 <external-path name="my_images" 
   path="Android/data/com.example.package.name/files/Pictures" />
</paths>

Note: Make sure that you replace com.example.package.name with the actual package name of your app.

5) Obtain the imageFilePath

if (requestCode == CAMERA_REQUEST) {   
        Bitmap bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), currentPhotoPath);
       imageView.setImageBitmap(bitmap); 
      // or you can use Glide to show image
      Glide.with(this).load(currentPhotoPath).into(imageView);
  }

Hope it works as you expect. for details you can see google Official documents!

Jakir Hossain
  • 3,830
  • 1
  • 15
  • 29