使用我的应用程序,用户将图像上传到 firestore,随后 firebase 数据库检索它。一切正常。图像存储在 firestore 中,url 链接在数据库中访问。但是,当我复制链接以在浏览器中打开时,出现此错误。
{ “错误”: { “代码”:403, “消息”:“权限被拒绝。” } }
有什么理由吗?
这是我的代码:
class ImageVerificationScreen extends StatefulWidget {
const ImageVerificationScreen({super.key});
@override
State<ImageVerificationScreen> createState() =>
_ImageVerificationScreenState();
}
class _ImageVerificationScreenState extends State<ImageVerificationScreen> {
File? image;
String imageUrl = '';
Future<File> customCompressed(
{required File imagePathToCompress,
quality = 100,
percentage = 10}) async {
var path = await FlutterNativeImage.compressImage(
imagePathToCompress.absolute.path,
quality: 100,
percentage: 80);
return path;
}
Future<File?> pickImages(BuildContext context) async {
File? image;
try {
final pickedImage =
await ImagePicker().pickImage(source: ImageSource.camera);
if (pickedImage != null) {
image = File(pickedImage.path);
File compressedImage =
await customCompressed(imagePathToCompress: image);
setState(() {
image = compressedImage;
});
}
} catch (e) {
showSnackBar(context, e.toString());
}
return image;
}
void uploadImage() async {
String uid = FirebaseAuth.instance.currentUser!.uid;
Reference ref = FirebaseStorage.instance.ref();
Reference refDirImage = ref.child('Documents');
Reference refImageUpload = refDirImage.child(uid);
try {
await refImageUpload.putFile(File(image!.path));
imageUrl = await refImageUpload.getDownloadURL();
} catch (error) {
return;
}
}
Future<String?> documentID() async {
String uid = FirebaseAuth.instance.currentUser!.uid;
CollectionReference document =
FirebaseFirestore.instance.collection('users');
document.doc(uid).collection('Documents').add({'Document': imageUrl});
return 'Documant uploaded';
}
// for selecting image
void selectImage() async {
image = await pickImages(context);
}
@override
Widget build(BuildContext context) {
final screenHeight = MediaQuery.of(context).size.height;
final screenWidth = MediaQuery.of(context).size.width;
return SafeArea(
child: Scaffold(
extendBodyBehindAppBar: true,
appBar: AppBar(
centerTitle: true,
elevation: 0,
backgroundColor: Colors.transparent,
leading: IconButton(
onPressed: () {
Navigator.pop(context);
},
icon: const Icon(
Icons.arrow_back,
size: 35,
color: Colors.blue,
),
),
),
body: SingleChildScrollView(
child: Column(
children: [
SizedBox(
height: screenHeight * 0.05,
),
const Text(
'Document',
style: TextStyle(fontSize: 25, fontWeight: FontWeight.bold),
),
InkWell(
onTap: () => selectImage(),
child: image == null
? SizedBox(
height: screenHeight * 0.5,
width: screenWidth,
child: const Icon(
Icons.camera_alt_outlined,
size: 70,
),
)
: Center(
child: Container(
height: screenHeight * 0.5,
width: screenWidth * 0.9,
decoration: BoxDecoration(
image: DecorationImage(
image: FileImage(image!),
fit: BoxFit.cover,
),
// your own shape
shape: BoxShape.rectangle,
),
),
),
),
if (image == null)
const Text(
'Click on the camera icon above to the a photo',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 18, fontWeight: FontWeight.bold),
),
SizedBox(
height: screenHeight * 0.2,
),
if (image != null)
TextButton(
onPressed: () {
uploadImage();
documentID();
},
child: const Text(
'Upload document',
style: TextStyle(fontSize: 20, fontWeight: FontWeight.bold),
))
],
)),
),
);
}
}