0

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.

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.

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:

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.

Thanks in advance, Jeff

jhooton
  • 1
  • 1
  • 2

1 Answers1

1

Uri.getPath() doesn't return the full URI, but only the path component. You'll need to use Uri.toString() instead. See https://stackoverflow.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.

Community
  • 1
  • 1
jonnydee
  • 1,311
  • 1
  • 7
  • 11
  • Okay sounds good. Yeah I'm realized that I need to have that picture/file uploaded to a backend somewhere if I'm to be able to access it from other devices. – jhooton Mar 18 '15 at 15:00