Android Firebase下载声音

我在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:没有身份验证令牌的请求

回答:

您存储的安全规则可能有一个

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; 

这样做只是为了测试,因为这主要是公共接入

然后,你可以调整你的身份验证方法根据你的规则。

如果你的规则不是我指定的内容,请分享你的规则,这样我们可以指导你因此而不是投机和猜测

以上是 Android Firebase下载声音 的全部内容, 来源链接: utcz.com/qa/267217.html

回到顶部