从JSON对象中拉出Uri后,ResolveUri在错误的位图Uri上失败

时间:2021-09-05 18:27:51

So my app is using Firebase to store user data which includes an avatar. I have the user select an image from the gallery and then I set my ImageView to the Uri of that file, which works fine.

所以我的应用程序使用Firebase来存储包含头像的用户数据。我让用户从库中选择一个图像然后我将我的ImageView设置为该文件的Uri,这很好。

When I then push that Uri.toString(); to Firebase and then try to read it again(in order to have the Avatar persist across sessions), I get a "resolveUri failed on bad bitmap uri" error.

当我然后推Uri.toString();到Firebase,然后尝试再次阅读(为了让Avatar在会话中保持不变),我得到一个“resolveUri在坏位图uri上失败”错误。

This is the code from where I am setting the image chosen from the gallery originally:

这是我设置从图库中选择的图像的代码:

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    if (requestCode == 1 && intent != null && resultCode == Activity.RESULT_OK) {
        Uri uri = intent.getData();
        Firebase usersRef = firebase.child("users").child("dummyUID").child("avatar");
        usersRef.setValue(uri.getPath());
        avatarIV.setImageURI(uri);
    } else {
        Log.d("Status:" , "Photo Picker Cancelled");
    }
}

And then this is the code I am using to try and pull that same Image Uri from Firebase, which fails:

然后这是我用来尝试从Firebase中拉出相同的Image Uri的代码,它失败了:

avatarRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            Uri uri = Uri.parse(dataSnapshot.getValue().toString());
            avatarIV.setImageURI(uri);
        }

        @Override
        public void onCancelled(FirebaseError firebaseError) {

        }
    });

I have put in log statements to figure out what could be different with the path, however it is the exact same in both locations.

我已经放入了日志语句来确定路径可能有什么不同,但是在两个位置都是完全相同的。

Here are the permissions I have in my manifest:

以下是我在清单中的权限:

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.MANAGE_DOCUMENTS"/>

I have tried almost every fix suggested in other threads that I could find to no avail. I figured this was a slightly different case scenario as I was reading the Uri back from Firebase.

我已经尝试了其他线程中建议的几乎所有修复,我发现无济于事。我认为这是一个略有不同的情况,因为我正在从Firebase读回Uri。

Thanks in advance, Jeff

杰夫先生,谢谢你

1 个解决方案

#1


1  

Uri.getPath() doesn't return the full URI, but only the path component. You'll need to use Uri.toString() instead. See https://*.com/a/17356461/1532344.

Uri.getPath()不返回完整的URI,只返回路径组件。你需要使用Uri.toString()代替。请参阅https://*.com/a/17356461/1532344。

Also note that this approach will not work across multiple devices, because the URI you get from the intent will point to a local file that is only available on that device.

另请注意,此方法不适用于多个设备,因为从intent获取的URI将指向仅在该设备上可用的本地文件。

#1


1  

Uri.getPath() doesn't return the full URI, but only the path component. You'll need to use Uri.toString() instead. See https://*.com/a/17356461/1532344.

Uri.getPath()不返回完整的URI,只返回路径组件。你需要使用Uri.toString()代替。请参阅https://*.com/a/17356461/1532344。

Also note that this approach will not work across multiple devices, because the URI you get from the intent will point to a local file that is only available on that device.

另请注意,此方法不适用于多个设备,因为从intent获取的URI将指向仅在该设备上可用的本地文件。