我在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:沒有身份驗證令牌的請求
說就在那裏,'爲request'沒有身份驗證令牌。其他請求是失敗還是僅僅是這個? – Phix