Android Firebase下载声音

听闻少年二字,当与平庸相斥。这篇文章主要讲述Android Firebase下载声音相关的知识,希望能为你提供帮助。
我在android应用程序上工作,我需要从firebase存储下载一些声音,我从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 createdcreated 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

我尝试更改Firebase> 存储中的规则,但没有任何效果。如果您有任何想法来解决我的问题。谢谢您帮忙。
编辑:
service firebase.storage { match /b/soundbox-a6dd8.appspot.com/o { match /{allPaths=**} { // Allow access by all users allow read, write: if true; } } }

新错误:E / firebase :; 已创建本地tem文件/storage/emulated/0/cjpcommodelouisxv/cjpcommodelouisxv.mp3 E / StorageUtil:获取令牌java.util.concurrent.ExecutionException:com.google.firebase.internal.api时出错。 FirebaseNoSignedInUserException:请在尝试获取令牌之前登录。 W / NetworkRequest:请求没有身份验证令牌
答案【Android Firebase下载声音】您的存储安全规则可能有一个
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); } }); }

同样在Firebase控制台中,在身份验证> 登录方法下,启用匿名登录
这可以解决您的问题
或者,如果您不希望对测试进行身份验证,请遵循以下规则:
allow write: if true;

这只是测试,因为这是基本的公共访问
然后,您可以根据您的规则调整身份验证方法。
如果您的规则不是我指定的规则,请分享您的规则,以便我们可以相应地指导您,而不是猜测和猜测

    推荐阅读