即使应用程序重新启动也保存图像 [英] Save the image even the application restart

查看:70
本文介绍了即使应用程序重新启动也保存图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有问题.每当我从图库中搜索图片以查找个人资料时,图片都会显示其作品.即使当我按下注销按钮并再次登录时,仍然有我的个人资料图片.但是,当我关闭应用程序并重新启动应用程序时,个人资料图片消失了.有人知道如何正确保存网址吗?

I have a problem. Everytime when I search a image from my gallery for my profilepicture its work. even when I pressed the log out button and log in again there is still my profilpicture. But when I closed the app and I restart the app then the profilepicture is gone. Does anyone know how to save the URL correctly?

 Future uploadPic(BuildContext context) async {
    String fileName = basename(_image.path);
    StorageReference firebaseStorageRef = FirebaseStorage.instance.ref().child(
        fileName);
    StorageUploadTask uploadTask = firebaseStorageRef.putFile(_image);
    StorageTaskSnapshot taskSnapshot = await uploadTask.onComplete;
    var downUrl = await (await uploadTask.onComplete).ref.getDownloadURL();
    Constants.URL_Profil_Picture = downUrl.toString();


    setState(() {
      print('Profile Picture uploaded');
      Scaffold.of(context).showSnackBar(
          SnackBar(content: Text('Profile Picture Uploaded')));
    });
  }

  image: (_image != null) ? FileImage(
                              _image)
                              : NetworkImage(Constants.URL_Profil_Picture),

推荐答案

这似乎是问题的原因:

Constants.URL_Profil_Picture = downUrl.toString();

我猜想,当您重新启动应用程序时,Constants.URL_Profil_Picture会再次设置为其硬编码值.

My guess it that when you restart the app, the Constants.URL_Profil_Picture is set to its hard-coded value again.

您需要将URL存储在应用程序重启后可以幸免的地方.常见的方法是:

You'll need to store the URL somewhere that survives application restarts. The common approaches for that are:

  1. 将URL存储在共享的首选项中,或其他一些URL中本地存储形式.这样一来,URL仅在从其上传的设备上可用.

  1. Store the URL in shared preferences, or some other form of local storage. This makes the URL available only on the device where it was uploaded from.

将URL存储在云数据库中,例如Cloud Firestore或实时数据库.这是最常见的选项,因为它为您提供了最大的灵活性.

Store the URL in a cloud database, such as Cloud Firestore or Realtime Database. This is the most common option, as it gives you the most flexibility.

鉴于您正在谈论用户个人资料:如果您正在使用Firebase身份验证登录用户,则 该图片是他们的个人资料图片,您还可以将其存储在用户个人资料中.

Given that you're talking about user profiles: if you're using Firebase Authentication to sign in the user, and this image is their profile picture, you can also store it in the user profile.

这篇关于即使应用程序重新启动也保存图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆