1

I am trying to take a pic from cam in my app, but it is working fine in but not able to display it in imageview it's throwing error..

please help me

my code is

It is giving error at the time of Decoding bitmapUri (i mentioned in code)

the error is:

private void onCaptureImageResult(Intent data) {

          Log.w("CAM","capture image result");
          launchMediaScanIntent();

          try {
              File photo = new File(Environment.getExternalStorageDirectory(), "picture.jpg");
              Log.w("CAM","decoding bitmap uri");
              Context context=FSE_login.this;
              Bitmap bitmap = decodeBitmapUri(this, FileProvider.getUriForFile(context, context.getApplicationContext().getPackageName() + ".lerainfotech.tezmoney.FSE_login", photo));
              Log.w("CAM","decoded");
              attendance_pic = Utils.getResizedBitmap(bitmap);
              Log.w("CAM","attached");
              attendance_pic_field.setImageBitmap(attendance_pic);
          } catch (Exception e) {

              Log.w("Error Is",e);
              Utils.showErrorDialog(getApplicationContext(), "Couldn't load image");
              return;
          }
      }
Prasanth Yejje
  • 267
  • 1
  • 3
  • 10
  • My Error Is: java.io.FileNotFoundException: open failed: ENOENT (No such file or directory) at android.os.ParcelFileDescriptor.openInternal(ParcelFileDescriptor.java:313) at android.os.ParcelFileDescriptor.open(ParcelFileDescriptor.java:211) at android.support.v4.content.FileProvider.openFile(FileProvider.java:544) at android.content.ContentProvider.openAssetFile(ContentProvider.java:1538 – Prasanth Yejje Nov 08 '17 at 13:14

1 Answers1

0

First you need to write below code in Mainifest

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

Your Activity should like this

    if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(this, new String[] { Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE }, 0);
    }else{
        captureImage();
    }
}

@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
    if (requestCode == 0) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED
                && grantResults[1] == PackageManager.PERMISSION_GRANTED) {
            captureImage();
        }
    }
}
private void captureImage(){
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    File photo = new File(Environment.getExternalStorageDirectory(), "picture.jpg");
    imageUri = Uri.fromFile(photo);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    startActivityForResult(intent, REQUEST_CAMERA);
}
Munir
  • 2,548
  • 1
  • 11
  • 20
  • it is giving this error brother: android.os.FileUriExposedException: file:///storage/emulated/0/picture.jpg exposed beyond app through ClipData.Item.getUri() – Prasanth Yejje Nov 08 '17 at 06:44
  • 1
    @PrasanthYejje it's just because you targeting 24 or higher api level try this link https://stackoverflow.com/questions/38200282/android-os-fileuriexposedexception-file-storage-emulated-0-test-txt-exposed – Munir Nov 08 '17 at 06:49