Prateek Chandan Prateek Chandan - 26 days ago 6
Android Question

Get Texture from google play game services Icon Uri in Libgdx

I am trying to show user profile icon in my game in libgdx. In the android side of code, i can get the image uri for the player using

mParticipants.get(i).getIconImageUri()
which gives me the Uri. But I can't find a way to convert the Uri to Texture which i can consume in my libgdx code.

I tried first getting the bitmap from Uri using the code

Bitmap bitmap = null;
try {
InputStream inputStream = application.getContentResolver().openInputStream(imgUri);
bitmap = BitmapFactory.decodeStream(inputStream);
} catch (FileNotFoundException e) {
e.printStackTrace();
}


But this gives me a security error.

Other answers on google shows me the ways, all of which include the ways to show image in an Android ImageView but i need a texture which i can use in my libgdx game to show the image

I have also tried adding following permission to my manifest

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


Even then i am still getting security error. Please help me to resolve this issue

Answer Source

After a lot of research it seemed to me that it is Not Possible to get the bitmap by reading the input stream for the Uri. The only way to do this is using the ImageManager

The sample code for achieving this is:

private void SetUserIconForPlayer(final Player player, Uri uri)
{
    ImageManager mgr = ImageManager.create(application);
    mgr.loadImage(new ImageManager.OnImageLoadedListener() {
        @Override
        public void onImageLoaded(Uri uri,final Drawable drawable, boolean isRequestedDrawable) {
            if(isRequestedDrawable)
            {
                Gdx.app.postRunnable(new Runnable() {
                    @Override
                    public void run() {
                        player.SetUserIcon(GetTextureFromDrawable(drawable));
                    }
                });
            }
        }
    }, uri);
}