【发布时间】:2017-11-02 01:14:13
【问题描述】:
使用下面的代码,我可以看到上传到我的 firebase 存储帐户的文件未经身份验证,但其他文件无法上传。
UploadFileAsync upload_task;
for(int i=0; i<Files.size(); i++)
{
upload_task=new UploadFileAsync(getApplicationContext());
upload_task.filePath=Files.get(i);
upload_task.execute();
count++;
}
public class UploadFileAsync extends AsyncTask<String, Void, Boolean> {
public String filePath;
public boolean isUploaded=false;
double progress;
public Context context;
private StorageReference storageReference;
boolean res = false;
//this method will upload the file
private boolean uploadFile(final Context context, final String filePath) {
//if there is a file to upload
if (filePath != null) {
//getting firebase storage reference
storageReference = FirebaseStorage.getInstance().getReference();
StorageReference riversRef = storageReference.child(filePath);
riversRef.putFile(Uri.fromFile(new File(fileName)))
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
//if the upload is successfull
isUploaded=true;
}
})
.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
//if the upload is not successfull
isUploaded=false;
}
})
.addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
@Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
//calculating progress percentage
progress = (100.0 * taskSnapshot.getBytesTransferred()) / taskSnapshot.getTotalByteCount();
Log.e("Progress"+fileName,""+progress);
});
}
//if there is not any file
else {
//you can display an error toast
}
return isUploaded;
}
public UploadFileAsync (Context context){
this.context = context;
this.isUploaded=false;
}
@Override
protected Boolean doInBackground(String... params) {
isUploaded = uploadFile(this.context, filePath);
return isUploaded;
}
@Override
protected void onPostExecute(Boolean result) {
}
@Override
protected void onPreExecute() {
}
@Override
protected void onProgressUpdate(Void... values) {
}
}
我得到这个错误,即使我在 AsyncTask 之外调用上传函数:
StorageUtil:获取令牌时出错 java.util.concurrent.ExecutionException:com.google.android.gms.internal.zzbqn:请先登录,然后再尝试获取令牌。
【问题讨论】:
标签: android firebase firebase-authentication firebase-storage