Question

I am doing an Android application which need to open camera and display picture on screen and take that picture absolute path.

But I always get "UnsupportedOperationException: Unknown URI: content://media/external/images/media" error when I click on camera.

My code:

        ContentValues values = new ContentValues();
        values.put(MediaStore.Images.Media.TITLE, "ircms");
        imgUri = getContentResolver().insert(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); // Here getting error
        Intent intentPicture = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        intentPicture.putExtra(MediaStore.EXTRA_OUTPUT, imgUri);
        startActivityForResult(intentPicture, 1);

onActivityResult:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
        case 1:
            try {
                String photoPath = getRealPathFromURI(this, imgUri);
                o2.inSampleSize = 8;
                Bitmap bitmap = BitmapFactory.decodeFile(photoPath, o2);
                }
       }
}

Please Help...

Was it helpful?

Solution

try this

What Do you Need In Manafiest.xml

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

In your activity

private static final int TAKE_PHOTO_CODE = 1;

private void takePhoto(){
  final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
  intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getTempFile(this)) ); 
  startActivityForResult(intent, TAKE_PHOTO_CODE);
}

private File getTempFile(Context context){
  //it will return /sdcard/image.tmp
  final File path = new File( Environment.getExternalStorageDirectory(), context.getPackageName() );
  if(!path.exists()){
    path.mkdir();
  }
  return new File(path, "image.tmp");
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  if (resultCode == RESULT_OK) {
    switch(requestCode){
      case TAKE_PHOTO_CODE:
        final File file = getTempFile(this);
        try {
          Bitmap captureBmp = Media.getBitmap(getContentResolver(), Uri.fromFile(file) );
          // do whatever you want with the bitmap (Resize, Rename, Add To Gallery, etc)
        } catch (FileNotFoundException e) {
          e.printStackTrace();
        } catch (IOException e) {
          e.printStackTrace();
        }
      break;
    }
  }
}

OTHER TIPS

Use this code to get file path of the image in onActivityResult

Uri selectedImageURI = data.getData();
imageFile = new File(getRealPathFromURI(selectedImageURI));

private String getRealPathFromURI(Uri contentURI) {
    Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
    if (cursor == null) { // Source is Dropbox or other similar local file path
        return contentURI.getPath();
    } else { 
        cursor.moveToFirst(); 
        int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA); 
        return cursor.getString(idx); 
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top