2016-07-14 84 views
0

在我的Activity中,我使用FireBase從存儲中下載了我的遊戲圖片。 如果我在我的Activity上運行這個代碼,但是如果我在我的關卡類中使用它作爲方法,它只會返回null。從android的firebase存儲中獲取圖片

這是我的活動:

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

任何人都可以請幫我找出什麼錯誤?

+1

什麼是返回null? Level類中的方法是'void'。你有一個logcat或調試打印? – Bill

+0

對不起,我不清楚。這個命令:「」「pictureRef.getFile(f).addOnSuccessListener(new OnSuccessListener ()」 當它在這個Level級別中不會返回我的firebase中的圖像 – Adi

回答

0

您正在Level級別下載不同的參考文件,所以這看起來像是一個蘋果到橙色的比較。

嘗試添加addOnFailure除了addOnSuccess,因爲我懷疑你正在獲得這些其他文件的404。

相關問題