Android 如何将文件Uri方案转换为内容Uri方案?

Android 如何将文件Uri方案转换为内容Uri方案?,android,Android,在android中,当您从库中获取uri时,它的值将以content://blahblahblah.blahblah.format,但如果您从手机摄像头获取uri,它将以文件开始:// 下面是我想做的: private File uriToBitmap(Uri uri, int maxSize) throws FileNotFoundException { try { imageStream = getContentResolver().openInputStream(uri

在android中,当您从库中获取uri时,它的值将以
content://blahblahblah.blahblah.format
,但如果您从手机摄像头获取uri,它将以
文件开始://

下面是我想做的:

private File uriToBitmap(Uri uri, int maxSize) throws FileNotFoundException {
  try {
        imageStream = getContentResolver().openInputStream(uri);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
    Bitmap claimBitmap = BitmapFactory.decodeStream(imageStream);
   }
在这个方法中,我希望传递一个文件类型uri并使用getContentResolver()函数,但不幸的是claimBitmap是一个
null
,这是否意味着getContentResolver()方法不接受文件类型uri?请帮助。

请按照此操作。它会帮助你的 如果您存储为本地驱动器

    String filePath = null;
    Uri _uri = data.getData();
    Log.d("","URI = "+_uri);
    if(_uri!=null&&"content".equals(_uri.getScheme())){
    Cursor cursor=this.getContentResolver().query(_uri,new String[]{android.provider.MediaStore.Images.ImageColumns.DATA},null,null,null);
    cursor.moveToFirst();
    filePath=cursor.getString(0);
    cursor.close();
    }else
    {
    filePath=_uri.getPath();
    }

否,ContentResolver支持文件方案,如果不支持该方案的uri,此方法将抛出FileNotFoundException。请检查一下电话号码

public final InputStream openInputStream(Uri uri)
        throws FileNotFoundException {
    String scheme = uri.getScheme();
    if (SCHEME_ANDROID_RESOURCE.equals(scheme)) {
        // Note: left here to avoid breaking compatibility.  May be removed
        // with sufficient testing.
        OpenResourceIdResult r = getResourceId(uri);
        try {
            InputStream stream = r.r.openRawResource(r.id);
            return stream;
        } catch (Resources.NotFoundException ex) {
            throw new FileNotFoundException("Resource does not exist: " + uri);
        }
    } else if (SCHEME_FILE.equals(scheme)) {
        // Note: left here to avoid breaking compatibility.  May be removed
        // with sufficient testing.
        return new FileInputStream(uri.getPath());
    } else {
        AssetFileDescriptor fd = openAssetFileDescriptor(uri, "r", null);
        try {
            return fd != null ? fd.createInputStream() : null;
        } catch (IOException e) {
            throw new FileNotFoundException("Unable to create stream");
        }
    }
}

这是ContentResolver的源代码,希望它能帮助您

这应该是一个注释,那么我怎么能简单地将文件uri方案转换成位图呢?