简体   繁体   English

如何将图像字符串从Firebase数据库转换为Android中的位图?

[英]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. 我正在尝试从Firebase检索照片网址并将其转换为位图。 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. 上传到Firebase并从Firebase存储中检索图像以在listview中显示是没有问题的,因为Glide库正在转换网址并将图像放入ImageView。 For a bitmap texture in OpenGL though, Glide isn't appropriate. 但是对于OpenGL中的位图纹理,Glide是不合适的。 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 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. 在logcat中,它显示了检索到的URL。 In that format though. 但是那种格式。 I save the url to with SharedPreferences. 我使用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. 我使用Rajawali OpenGL框架来显示一个3d模型,它从保存到SharedPreferences的图像URL获取它的位图。 I use this async class to convert the url to bitmap and apply it to the 3d models texture. 我使用这个异步类将url转换为位图并将其应用于3d模型纹理。

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: 然后我在我的渲染器onRender()方法中调用它:

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. 当Rajawali框架尝试应用转换后的位图时,它表示url字符串为null / empty。

This is the referenced method from UserPrefs.getCustomLabel(): 这是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. Rajawali很有可能向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. 常见的解决方案是使用所谓的下载URL来检索图像。 A download URL is a publicly accessible, but unguessable, URL. 下载URL是可公开访问但不可取的URL。 You can get such URL from a Firebase Storage URL by using this code snippet from the Firebase documentation on downloading files : 您可以使用Firebase文档中有关下载文件的代码段从Firebase存储网址获取此类网址:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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