如何在Android中将Firebase数据库中的图像字符串转换为位图?
我正在尝试从Firebase检索照片url并将其转换为位图。上载到Firebase并从Firebase存储中检索图像以显示在listview中没有问题,因为Glide库正在转换url并将图像放置到ImageView中。不过,对于OpenGL中的位图纹理,Glide并不合适。照片url如下所示(已更改关键信息以保持安全性,但格式相同): 在logcat中,它显示检索到的url。不过是那种格式。我使用SharedReferences将url保存到。我正在使用Rajawali OpenGL框架显示一个3d模型,该模型从保存到SharedReferences的图像url获取位图。我使用这个异步类将url转换为位图,并将其应用于3d模型纹理如何在Android中将Firebase数据库中的图像字符串转换为位图?,android,firebase,firebase-storage,rajawali,Android,Firebase,Firebase Storage,Rajawali,我正在尝试从Firebase检索照片url并将其转换为位图。上载到Firebase并从Firebase存储中检索图像以显示在listview中没有问题,因为Glide库正在转换url并将图像放置到ImageView中。不过,对于OpenGL中的位图纹理,Glide并不合适。照片url如下所示(已更改关键信息以保持安全性,但格式相同): 在logcat中,它显示检索到的url。不过是那种格式。我使用SharedReferences将url保存到。我正在使用Rajawali OpenGL框架显示一
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);
}
}
当Rajawali框架尝试应用转换后的位图时,它表示url字符串为null/空
这是UserPrefs.getCustomLabel()中引用的方法:
Rajawali很有可能向Firebase Storage发出未经验证的请求。如果使用默认安全规则,则未经验证的请求将被拒绝 常见的解决方案是使用所谓的下载URL来检索图像。下载URL是一个可公开访问但不可用的URL。您可以使用以下代码段从Firebase存储URL获取此URL:
storageRef.child(“users/me/profile.png”).getDownloadUrl().addOnSuccessListener(新的OnSuccessListener()){
@凌驾
成功时的公共无效(Uri){
//已获取“users/me/profile.png”的下载URL
}
}).addOnFailureListener(新的OnFailureListener(){
@凌驾
public void onFailure(@NonNull异常){
//处理任何错误
}
});
Rajawali很有可能向Firebase Storage发出未经验证的请求。如果使用默认安全规则,则未经验证的请求将被拒绝
常见的解决方案是使用所谓的下载URL来检索图像。下载URL是一个可公开访问但不可用的URL。您可以使用以下代码段从Firebase存储URL获取此URL:
storageRef.child(“users/me/profile.png”).getDownloadUrl().addOnSuccessListener(新的OnSuccessListener()){
@凌驾
成功时的公共无效(Uri){
//已获取“users/me/profile.png”的下载URL
}
}).addOnFailureListener(新的OnFailureListener(){
@凌驾
public void onFailure(@NonNull异常){
//处理任何错误
}
});
那么,你的问题实际上与Firebase无关,更与将纹理加载到OpenGL视图无关?请显示你的代码中putString(LABEL\u KEY,…)
的位置。@cricket\u 007不,我现在知道如何加载纹理并将其应用到OpenGL中的3d模型。我的问题是关于Firebase的,因为我认为转换为位图的url/字符串不是有效的位图,因为在我上面的示例中.jpg后面有额外的文本。所以,你的问题与Firebase无关,更与将纹理加载到OpenGL视图无关?请显示你的putString(LABEL_KEY,…)
在您的代码中。@cricket\u 007不,我现在知道如何加载纹理并将其应用于OpenGL中的三维模型。我的问题是关于Firebase的,因为我认为转换为位图的url/字符串不是有效的位图,因为在我上面的示例中.jpg后面有额外的文本。
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();
}
}
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;
}
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
}
});