简体   繁体   中英

How to convert image string from Firebase Database to bitmap in Android?

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):

https://firebasestorage.googleapis.com/v0/b/api-project-123456789000.appspot.com/o/jzL123yO1ZxTZ1Se4ldYk2YD92v1%2Ffull%2F1234572738682%2Fcropped.jpg?alt=media&token=1e6cad76-1234f-4f9d-8923-7af07015da7d

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.

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<String, String, Bitmap> {
    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);
    }
}

I then call this in my renderers onRender() method:

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();
    }

    }

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

This is the referenced method from UserPrefs.getCustomLabel():

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;
}

There is a very good chance that Rajawali is making an unauthenticated request to Firebase Storage. If you use the default security rules, that unauthenticated request will be denied.

The common solution is to use a so-called download URL to retrieve the image. A download URL is a publicly accessible, but unguessable, URL. You can get such URL from a Firebase Storage URL by using this code snippet from the Firebase documentation on downloading files :

storageRef.child("users/me/profile.png").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
    @Override
    public void onSuccess(Uri uri) {
        // Got the download URL for 'users/me/profile.png'
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // Handle any errors
    }
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM