How to convert image string from Firebase Database to bitmap in Android?
<p>I am trying to retrieve photo url from Firebase and convert it to bitmap. Uploading to Firebase and retrieving images from Firebase storage to show in listview is no problem because Glide library is converting the url and placing the image into an ImageView. For a bitmap texture in OpenGL though, Glide isn't appropriate. The photo url comes out like this (key information has been altered to preserve security but the format is the same): </p>

<blockquote>
  <p><a href="https://firebasestorage.googleapis.com/v0/b/api-project-123456789000.appspot.com/o/jzL123yO1ZxTZ1Se4ldYk2YD92v1%2Ffull%2F1234572738682%2Fcropped.jpg?alt=media&amp;token=1e6cad76-1234f-4f9d-8923-7af07015da7d" rel="nofollow">https://firebasestorage.googleapis.com/v0/b/api-project-123456789000.appspot.com/o/jzL123yO1ZxTZ1Se4ldYk2YD92v1%2Ffull%2F1234572738682%2Fcropped.jpg?alt=media&amp;token=1e6cad76-1234f-4f9d-8923-7af07015da7d</a></p>
</blockquote>

<p>In the logcat it shows the retrieved url. In that format though. I save the url to with SharedPreferences. I am using the Rajawali OpenGL framework to show a 3d model which gets it's bitmap from the image url that was saved to SharedPreferences. I use this async class to convert the url to bitmap and apply it to the 3d models texture.</p>

<pre><code>import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.util.Log;

import org.rajawali3d.materials.textures.Texture;
import org.rajawali3d.materials.textures.TextureManager;

public class AsyncLoadImageTask  extends AsyncTask&lt;String, String, Bitmap&gt; {
    private final static String TAG = "AsyncTaskLoadImage";
    private Texture texture;
    public AsyncLoadImageTask(Texture texture) {
        this.texture = texture;
    }
    @Override
    protected Bitmap doInBackground(String... params) {
        Bitmap bitmap = null;
        try {
            URL url = new URL(params[0]);
            bitmap = BitmapFactory.decodeStream((InputStream)url.getContent());
        } catch (IOException e) {
            Log.e(TAG, e.getMessage());
        }
        return bitmap;
    }
    @Override
    protected void onPostExecute(Bitmap bitmap) {
        texture.setBitmap(bitmap);
        TextureManager.getInstance().replaceTexture(texture);
    }
}
</code></pre>

<p>I then call this in my renderers onRender() method:</p>

<pre><code>try {
        //This is reference to the String containing the url
        String url = UserPrefs.getCustomLabel(mContext);
        new AsyncLoadImageTask(labelTex).execute(url);
    } catch (Exception e) {
        e.printStackTrace();
    }

    }
</code></pre>

<p>When the Rajawali framework tries to apply the converted bitmap, it says that the url string is null/empty.</p>

<p>This is the referenced method from UserPrefs.getCustomLabel():</p>

<pre><code>public static String getCustomLabel(Context context){
    SharedPreferences settings;
    String text;
    settings = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); //1
    text = settings.getString(LABEL_KEY, null); //2
    Log.d(UP_TAG, "Retrieved Url: " + text);
    return text;
}
</code></pre>