我正在尝试从Firebase下载文件。我已经完成了从Firebase设置云存储的所有要求,并打开了我的云存储:
rules_version = '2';
service firebase.storage {
match /b/{bucket}/o {
match /{allPaths=**} {
allow read, write;
}
}
}
我只有一个materialButton
,当单击该public class MainActivity extends AppCompatActivity {
MaterialButton mconvertButton;
FirebaseStorage mfirebaseStorage;
StorageReference mstorageReference;
StorageReference ref;
@Override
protected void onCreate(Bundle savedInstanceState) {
mconvertButton = findViewById(R.id.convertButton);
mconvertButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
download();
}
});
}
public void download(){
mstorageReference = FirebaseStorage.getInstance().getReference();
ref = mstorageReference.child("_HDbN_ugHkQ.wav");
ref.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
@Override
public void onSuccess(Uri uri) {
String url = uri.toString();
downloadFiles(MainActivity.this, "_HDbN_ugHkQ",
".wav", DIRECTORY_DOWNLOADS, url);
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
e.printStackTrace();
}
});
}
public void downloadFiles(Context context, String fileName, String fileExtension, String destinationDirectory, String url){
DownloadManager downloadManager = (DownloadManager) context.
getSystemService(Context.DOWNLOAD_SERVICE);
Uri uri = Uri.parse(url);
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
request.setDestinationInExternalFilesDir(context, destinationDirectory, fileName + fileExtension);
}
时,应该从云存储下载文件并将其存储在下载目录中。这是我的代码
W/StorageUtil: no auth token for request
W/NetworkRequest: no auth token for request
但是,每次单击按钮,我都会回来:
我试图清除我的应用程序数据,我试图卸载并重新安装我的应用程序。我多次检查我的应用程序仍连接到Firebase,但问题仍然存在。 您对如何解决这个问题有任何想法吗?
日志详细信息
import 'package:Auszeit/services/auszeit_icons_icons.dart';
import 'package:Auszeit/widgets/auszeitDrawer.dart';
import 'package:Auszeit/widgets/order/itemCard.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
class OrderPage extends StatefulWidget {
@override
_OrderPageState createState() => _OrderPageState();
}
class _OrderPageState extends State<OrderPage>
with SingleTickerProviderStateMixin {
@override
Widget build(BuildContext context) {
TabController _tabController;
ScrollController _scrollController;
@override
void initState() {
_tabController = TabController(vsync: this, length: 4);
_scrollController = ScrollController();
super.initState();
}
return DefaultTabController(
length: 4,
child: Scaffold(
floatingActionButton: FloatingActionButton(
onPressed: () {},
elevation: 15,
child: Icon(
Icons.shopping_basket,
color: Colors.green[800],
),
backgroundColor: Colors.white,
),
appBar: AppBar(
elevation: 0,
backgroundColor: Colors.white,
leading: Builder(
builder: (context) {
return IconButton(
icon: Icon(
Icons.menu,
color: Colors.grey,
),
onPressed: () => AuszeitDrawer.of(context).open(),
);
},
),
title: Text(
"Auszeit eSG",
style: TextStyle(
color: Colors.green[800],
fontWeight: FontWeight.bold,
),
),
),
body: Padding(
padding: const EdgeInsets.fromLTRB(0, 16, 0, 16),
child: Column(
mainAxisAlignment: MainAxisAlignment.start,
crossAxisAlignment: CrossAxisAlignment.start,
children: <Widget>[
Padding(
padding: const EdgeInsets.symmetric(horizontal: 20),
child: Text(
"Bestellen",
style: TextStyle(fontSize: 30, fontWeight: FontWeight.bold),
),
),
Container(
height: 50,
child: ListView(
controller: _scrollController,
shrinkWrap: false,
scrollDirection: Axis.horizontal,
children: <Widget>[
FlatButton(
onPressed: () => _tabController.animateTo(1),
child: Text(
"BRÖTCHEN",
style: TextStyle(
fontFamily: "Open Sans",
color: Colors.green[800],
fontWeight: FontWeight.bold,
fontSize: 20),
),
),
FlatButton(
onPressed: () => _tabController.animateTo(2),
child: Text(
"KALTGETRÄNKE",
style: TextStyle(
fontFamily: "Open Sans",
color: Colors.grey,
fontWeight: FontWeight.bold,
fontSize: 20),
),
),
FlatButton(
onPressed: () => _tabController.animateTo(3),
child: Text(
"HEIßGETRÄNKE",
style: TextStyle(
fontFamily: "Open Sans",
color: Colors.grey,
fontWeight: FontWeight.bold,
fontSize: 20),
),
),
FlatButton(
onPressed: () {},
child: Text(
"MILCHPRODUKTE",
style: TextStyle(
fontFamily: "Open Sans",
color: Colors.grey,
fontWeight: FontWeight.bold,
fontSize: 20),
),
)
],
),
),
Expanded(
child: Padding(
padding: const EdgeInsets.fromLTRB(20, 20, 20, 30),
child: TabBarView(controller: _tabController, children: [
CategoryTab(catId: "1"),
CategoryTab(catId: "2"),
CategoryTab(catId: "3"),
CategoryTab(catId: "4"),
]),
),
),
],
),
),
));
}
}
class CategoryTab extends StatelessWidget {
final String catId;
const CategoryTab({Key key, @required this.catId}) : super(key: key);
@override
Widget build(BuildContext context) {
return StreamBuilder(
stream: Firestore.instance
.collection('items')
.where('catid', isEqualTo: catId)
.snapshots(),
builder: (context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (snapshot.hasData) {
return GridView.builder(
gridDelegate:
SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 2),
shrinkWrap: true,
itemCount: snapshot.data.documents.length,
itemBuilder: (BuildContext context, int index) {
return ItemCard(
cost: snapshot.data.documents[index]["cost"] == null
? 1
: snapshot.data.documents[index]["cost"],
name: snapshot.data.documents[index]["name"],
desc: snapshot.data.documents[index]["desc"] == null
? "Keine Beschreibung vorhanden"
: snapshot.data.documents[index]["desc"],
imageUrl: snapshot.data.documents[index]["image_path"],
);
},
);
} else {
return Container(
height: 50, width: 50, child: CircularProgressIndicator());
}
});
}
}
答案 0 :(得分:0)
问题解决了。我忘记将downloadManager.enqueue(request);
添加到我的downloadFiles
函数中。