在我的活动中,我使用FireBase从存储中下载游戏的图片。 如果我在我的Activity上运行此代码它可以工作,但如果我在我的级别类中使用它作为方法,它只返回null。
这是我的活动:
pic = null;
answer = null;
option = null;
picture = null;
try {
answer = File.createTempFile("buttonanswer", "png");
StorageReference storageRef = storage.getReferenceFromUrl("gs://yougotit-8ce92.appspot.com");
StorageReference levelDifficultRef = storageRef.child("easy");
final StorageReference levelRef = levelDifficultRef.child(Game.LEVEL + 1);
StorageReference levelAnswerRef = levelRef.child("pic" + "." + "jpg");
levelAnswerRef.getFile(answer).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
answerV = new ImageView(getApplicationContext());
Bitmap myBitmap = BitmapFactory.decodeFile(answer.getPath());
answerV.setImageBitmap(myBitmap);
runOnUiThread(new Runnable() {
@Override
public void run() {
maingameLinarLayout.addView(answerV);
}
});
}
});
}
catch (IOException e) {
e.printStackTrace();
}
在关卡类中,这个我的代码不起作用并返回null。
这是我的关卡课程:
public class Level {
public static final String ANSWER="answer";
public static final String OPTION="ops";
public static final String PICTURE="pic";
public static final String QUESTION="question";
public static final String PNG = "png";
public static final String JPG = "jpg";
private File mainPicture,fadedPicture,opt1,opt2,opt3,answer,question;
private int flag;//in easy level when reach to 6 it indicate that that level is ready //in medium and hard level when reach to 7
private StorageReference storageRef;
private String difficult;
private Game game;
public Level(Game game,FirebaseStorage storageIns,String difficult,int level) {
flag = 0 ;
this.game = game;
this.difficult = difficult;
this.storageRef = storageIns.getReferenceFromUrl("gs://yougotit-8ce92.appspot.com");
StorageReference levelDifficultRef = storageRef.child(difficult);
final StorageReference levelRef = levelDifficultRef.child(Game.LEVEL+level);
if(difficult.equals(Game.LEVEL_HARD) ) {
}
if(difficult.equals(Game.LEVEL_MEDIUM) ) {
}
else {
downloadPicture(levelRef, mainPicture, PICTURE, JPG);
downloadPicture(levelRef, answer, ANSWER, PNG);
downloadPicture(levelRef, opt1, OPTION + 1, PNG);
downloadPicture(levelRef, opt2, OPTION + 2, PNG);
downloadPicture(levelRef, opt3, OPTION + 3, PNG);
}
}
private void downloadPicture(StorageReference levelRef,File f,String picName,String picFormat) {
StorageReference pictureRef = levelRef.child(picName+"."+picFormat);
f = null;
try {
f = File.createTempFile(picName,picFormat);
pictureRef.getFile(f).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
@Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
flag++;
if(flag == 5) {
activeCallBack();
}
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
}
有人可以帮我弄清楚出了什么问题吗?
答案 0 :(得分:0)
您正在Level类中下载不同的引用,因此这看起来像是橙色比较。
尝试添加addOnFailure以及addOnSuccess,因为我怀疑你为这些其他文件获得了404.