2017-12-27 1274 views
0

我在Android應用程序上工作,我需要從firebase存儲中下載一些聲音,我從firebase實時數據庫中獲取聲音名稱。Android Firebase下載聲音

我的代碼:

public class DataSyncFb extends AsyncTask<Void, Integer, Void> { 


private Context context; 
private StorageReference mStorageRef; 


public DataSyncFb(final Context context) { 
    this.context = context; 
} 

@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
    Log.i("DataSincFB", "onPreExecute"); 
} 


@Override 
protected Void doInBackground(Void... voids) { 
    final DatabaseHandler db = new DatabaseHandler(context); 
    final FirebaseDatabase database = FirebaseDatabase.getInstance(); 

    DatabaseReference dbRef = database.getReference("categorie"); 
    dbRef.addValueEventListener(new ValueEventListener() { 
     @Override 
     public void onDataChange(DataSnapshot dataSnapshot) { 
      try { 
       List<String> listCat = db.getAllCategoriesId(); 
       List<String> listSounds = db.getAllSoundsId(); 
       Log.i("test", String.valueOf(dataSnapshot.getValue())); 
       Gson test = new Gson(); 

       JSONArray jsonArray = new JSONArray(test.toJson(dataSnapshot.getValue())); 

       for (int i = 0; i < jsonArray.length(); i++){ 
        JSONObject cat = jsonArray.getJSONObject(i); 
        String nom = cat.getString("nom"); 
        String id = cat.getString("id"); 
        if (!listCat.contains(id)) { 
         db.addCategorie(nom, id); 
        } 

        JSONArray sounds = cat.getJSONArray("son"); 
        Log.i("cat", sounds.toString()); 
        for (int j = 0; j < sounds.length(); j++){ 
         JSONObject sound = sounds.getJSONObject(j); 
         String soundId = sound.getString("id"); 
         if (!listSounds.contains(soundId)){ 

          downloadSound(); 
          db.addSound(soundId, sound.getString("nom"), sound.getString("lien") ,id); 

         } 

        } 
       } 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 

     } 

     @Override 
     public void onCancelled(DatabaseError databaseError) { 

     } 
    }); 
    return null; 
} 

@Override 
protected void onPostExecute(Void aVoid) { 
    Log.i("DataSyncFB", "onPostExecute"); 
} 

private void downloadSound(){ 
    Log.v("download", "in function"); 

    try { 
     StorageReference islandRef = FirebaseStorage.getInstance().getReferenceFromUrl("gs://soundbox-a6dd8.appspot.com").child("cjpcommodelouisxv.mp3"); 


     File localFile = null; 

     localFile = File.createTempFile("cjpcommodelouisxv", "mp3"); 
     islandRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() { 
      @Override 
      public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) { 
       Log.v("download", "Success"); 
       // Local temp file has been created 
      } 
     }).addOnFailureListener(new OnFailureListener() { 
      @Override 
      public void onFailure(@NonNull Exception exception) { 
       // Handle any errors 
       Log.e("download", "Error"); 

      } 
     }); 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } 
} 

我得到這個錯誤:

E/StorageException: StorageException has occurred.                    User does not have permission to access this object.              Code: -13021 HttpResult: 403 
E/firebase: ;local tem file not created created com.google.firebase.storage.StorageException: User does not have permission to access this object. 
E/StorageUtil: error getting token java.util.concurrent.ExecutionException: com.google.firebase.internal.api.FirebaseNoSignedInUserException: Please sign in before trying to get a token. 
W/NetworkRequest: no auth token for request 

我試圖改變在火力地堡>存儲,但沒有工作。如果您有任何意見,以解決我的問題的規則。感謝您的幫助。

編輯:

service firebase.storage { 
match /b/soundbox-a6dd8.appspot.com/o { 
    match /{allPaths=**} { 
    // Allow access by all users 
    allow read, write: if true; 
    } 
} 
} 

新的錯誤: E /火力點:;本地TEM文件中創建創建/storage/emulated/0/cjpcommodelouisxv/cjpcommodelouisxv.mp3 E/StorageUtil:錯誤得到令牌的java.util .concurrent.ExecutionException:com.google.firebase.internal.api.FirebaseNoSignedInUserException:請在嘗試獲取令牌之前登錄。 W/NetworkRequest:沒有身份驗證令牌的請求

+0

說就在那裏,'爲request'沒有身份驗證令牌。其他請求是失敗還是僅僅是這個? – Phix

回答

0

您存儲的安全規則可能有一個

allow write: if request.auth !=null 

條款,在這裏你似乎沒有使用FirebaseAuth對用戶進行認證。

對於測試,建議您使用signInAnonymously()方法並在寫入存儲之前透明地對用戶進行簽名。

FirebaseAuth mAuth = FirebaseAuth.getInstance(); 

然後註冊在做任何東西之前,你正在做

FirebaseUser user = mAuth.getCurrentUser(); 
if (user != null) { 
    // do your stuff 
} else { 
    signInAnonymously(); 
} 

signInAnonymously()方法是在火力地堡控制檯

private void signInAnonymously(){ 
    mAuth.signInAnonymously().addOnSuccessListener(this, new OnSuccessListener<AuthResult>() { 
     @Override public void onSuccess(AuthResult authResult) { 
      // do your stuff 
     } 
    }) .addOnFailureListener(this, new OnFailureListener() { 
      @Override public void onFailure(@NonNull Exception exception) { 
        Log.e("TAG", "signInAnonymously:FAILURE", exception); 
      } 
    }); 
} 

也跟隨,在認證>登錄方法,啓用匿名登錄

這可能會解決您的問題

或者,如果你不想進行測試認證有規則:

allow write: if true; 

這樣做只是爲了測試,因爲這主要是公共接入

然後,你可以調整你的身份驗證方法根據你的規則。

如果你的規則不是我指定的內容,請分享你的規則,這樣我們可以指導你因此而不是投機和猜測

+0

我剛剛更新了我的文章 –

+0

您是否在嘗試任何此類操作之前嘗試匿名登錄? – Kushan

+0

如何以匿名登錄? –