【问题标题】:UnsupportedOperationException Error while opening camera android打开相机android时出现UnsupportedOperationException错误
【发布时间】:2023-03-23 12:00:01
【问题描述】:

我正在做一个 Android 应用程序,它需要打开相机并在屏幕上显示图片并获取该图片的绝对路径。

但是当我点击相机时,我总是收到"UnsupportedOperationException: Unknown URI: content://media/external/images/media" 错误。

我的代码:

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

请帮忙...

【问题讨论】:

  • 您是否在“Manifest 文件”中进行了更改以支持“媒体”、“外部存储”???
  • 是的,我添加了
  • 发生在模拟器还是真机上?
  • 真实设备。它适用于 lenovo Tab,但适用于 Zolo Android 手机。
  • 看我的回答!!!!你可能忘了给相机权限吗

标签: java android android-camera android-camera-intent android-bitmap


【解决方案1】:

试试这个

你需要什么 在 Manafiest.xml 中

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

在你的活动中

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

【讨论】:

  • 我正在尝试您的答案,但它要求我创建 Media.getBitmap();功能。
  • 你必须导入 android.provider.MediaStore.Images.Media
  • 不,你不需要!只需导入 android.provider.MediaStore.Images.Media
  • 如果你使用的是 eclipse 而不是按 ctrl+shift+O
【解决方案2】:

使用此代码获取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); 
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-11-11
    • 2012-02-24
    • 2023-01-13
    • 1970-01-01
    • 1970-01-01
    • 2014-02-06
    • 1970-01-01
    • 2019-08-01
    相关资源
    最近更新 更多