图片不是从 Cloud Firestore 加载的吗?
Images are not loading from cloud firestore?
我正在使用 firebase 制作一个简单的应用程序,用户可以在其中在 firebase 中添加图像,但问题是图像加载不正确。另外,我用了很多图像加载库,比如Picasso,Glide,Coil,但问题都没有解决。
AllPhotos.kt
class AllPhotos : AppCompatActivity() {
lateinit var appBar: MaterialToolbar
lateinit var appBarLayout: AppBarLayout
lateinit var drawerLayout: DrawerLayout
lateinit var addPostBtn: FloatingActionButton
lateinit var actionBarDrawerToggle: ActionBarDrawerToggle
lateinit var firebaseAuth: FirebaseAuth
lateinit var navView: NavigationView
lateinit var recyclerView: RecyclerView
lateinit var firestore: FirebaseFirestore
lateinit var collectionReference: CollectionReference
lateinit var postsList: ArrayList<Posts>
lateinit var photoAdapter: PhotoAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_all_photos)
firebaseAuth = FirebaseAuth.getInstance()
appBar = findViewById(R.id.topAppBar)
appBarLayout = findViewById(R.id.appBarLayout)
drawerLayout = findViewById(R.id.drawerLayout)
addPostBtn = findViewById(R.id.openAddPostScreen)
navView = findViewById(R.id.navView)
recyclerView = findViewById(R.id.recyclerView)
firestore = FirebaseFirestore.getInstance()
collectionReference = firestore.collection(firebaseAuth.currentUser!!.email.toString())
postsList = arrayListOf()
recyclerView = findViewById(R.id.recyclerView)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.setHasFixedSize(true)
setUpDrawerLayout()
addPostBtn.setOnClickListener {
val intent = Intent(this, AddPost::class.java)
startActivity(intent)
}
displayPosts()
navView.setNavigationItemSelectedListener {
when (it.itemId) {
R.id.profile -> {
drawerLayout.closeDrawers()
val intent = Intent(this, ProfileActivity::class.java)
startActivity(intent)
}
R.id.signOut -> {
firebaseAuth.signOut()
drawerLayout.closeDrawers()
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
finish()
Toast.makeText(this, "User signed out", Toast.LENGTH_SHORT).show()
}
}
true
}
navView.menu.getItem(0).isChecked = true
}
@SuppressLint("NotifyDataSetChanged")
private fun displayPosts() {
collectionReference.addSnapshotListener { value, error ->
when (error != null) {
true -> {
Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show()
}
false -> {
for (listDocument in value!!.documentChanges) {
if (listDocument.type == DocumentChange.Type.ADDED) {
postsList.add(listDocument.document.toObject(Posts::class.java))
Log.d(
"ALL_POSTS",
listDocument.document.toObject(Posts::class.java).toString()
)
}
}
photoAdapter = PhotoAdapter(this, postsList)
recyclerView.adapter = photoAdapter
photoAdapter.notifyDataSetChanged()
}
}
}
}
private fun setUpDrawerLayout() {
setSupportActionBar(appBar)
actionBarDrawerToggle =
ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setHomeButtonEnabled(true)
drawerLayout.addDrawerListener(actionBarDrawerToggle)
actionBarDrawerToggle.syncState()
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true
}
return super.onOptionsItemSelected(item)
}
}
Posts.kt
data class Posts(
var description: String = "",
var imageUri: String = "",
var postDate: String = ""
)
PhotoAdapter.kt
class PhotoAdapter(val context: Context, val postsList: ArrayList<Posts>) :
RecyclerView.Adapter<PhotoAdapter.PhotoViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PhotoViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.post_layout, parent, false)
return PhotoViewHolder(view)
}
override fun onBindViewHolder(holder: PhotoViewHolder, position: Int) {
val currentItem = postsList[position]
holder.descText.text = currentItem.description
Glide.with(context).load(currentItem.imageUri).into(holder.postImageView)
holder.dateText.text = currentItem.postDate
}
override fun getItemCount(): Int {
return postsList.size
}
class PhotoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val postImageView: ImageView = itemView.findViewById(R.id.postImages)
val dateText: TextView = itemView.findViewById(R.id.textDate)
val descText: TextView = itemView.findViewById(R.id.textDesc)
}
}
云 Firestore 结构的示例图像
截图中的imageUri
值以content://
开头,表示指向特定设备上的特定文件。如果您与其他用户共享 URL,该文件将不存在于他们的设备上 - 因此它没有任何内容可显示。
您要做的是upload the file from the source device to a cloud storage location, generate a download URL上传的文件,然后将下载的URL存储在数据库中。
我正在使用 firebase 制作一个简单的应用程序,用户可以在其中在 firebase 中添加图像,但问题是图像加载不正确。另外,我用了很多图像加载库,比如Picasso,Glide,Coil,但问题都没有解决。
AllPhotos.kt
class AllPhotos : AppCompatActivity() {
lateinit var appBar: MaterialToolbar
lateinit var appBarLayout: AppBarLayout
lateinit var drawerLayout: DrawerLayout
lateinit var addPostBtn: FloatingActionButton
lateinit var actionBarDrawerToggle: ActionBarDrawerToggle
lateinit var firebaseAuth: FirebaseAuth
lateinit var navView: NavigationView
lateinit var recyclerView: RecyclerView
lateinit var firestore: FirebaseFirestore
lateinit var collectionReference: CollectionReference
lateinit var postsList: ArrayList<Posts>
lateinit var photoAdapter: PhotoAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_all_photos)
firebaseAuth = FirebaseAuth.getInstance()
appBar = findViewById(R.id.topAppBar)
appBarLayout = findViewById(R.id.appBarLayout)
drawerLayout = findViewById(R.id.drawerLayout)
addPostBtn = findViewById(R.id.openAddPostScreen)
navView = findViewById(R.id.navView)
recyclerView = findViewById(R.id.recyclerView)
firestore = FirebaseFirestore.getInstance()
collectionReference = firestore.collection(firebaseAuth.currentUser!!.email.toString())
postsList = arrayListOf()
recyclerView = findViewById(R.id.recyclerView)
recyclerView.layoutManager = LinearLayoutManager(this)
recyclerView.setHasFixedSize(true)
setUpDrawerLayout()
addPostBtn.setOnClickListener {
val intent = Intent(this, AddPost::class.java)
startActivity(intent)
}
displayPosts()
navView.setNavigationItemSelectedListener {
when (it.itemId) {
R.id.profile -> {
drawerLayout.closeDrawers()
val intent = Intent(this, ProfileActivity::class.java)
startActivity(intent)
}
R.id.signOut -> {
firebaseAuth.signOut()
drawerLayout.closeDrawers()
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
finish()
Toast.makeText(this, "User signed out", Toast.LENGTH_SHORT).show()
}
}
true
}
navView.menu.getItem(0).isChecked = true
}
@SuppressLint("NotifyDataSetChanged")
private fun displayPosts() {
collectionReference.addSnapshotListener { value, error ->
when (error != null) {
true -> {
Toast.makeText(this, "Error", Toast.LENGTH_SHORT).show()
}
false -> {
for (listDocument in value!!.documentChanges) {
if (listDocument.type == DocumentChange.Type.ADDED) {
postsList.add(listDocument.document.toObject(Posts::class.java))
Log.d(
"ALL_POSTS",
listDocument.document.toObject(Posts::class.java).toString()
)
}
}
photoAdapter = PhotoAdapter(this, postsList)
recyclerView.adapter = photoAdapter
photoAdapter.notifyDataSetChanged()
}
}
}
}
private fun setUpDrawerLayout() {
setSupportActionBar(appBar)
actionBarDrawerToggle =
ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.close)
supportActionBar?.setDisplayHomeAsUpEnabled(true)
supportActionBar?.setHomeButtonEnabled(true)
drawerLayout.addDrawerListener(actionBarDrawerToggle)
actionBarDrawerToggle.syncState()
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true
}
return super.onOptionsItemSelected(item)
}
}
Posts.kt
data class Posts(
var description: String = "",
var imageUri: String = "",
var postDate: String = ""
)
PhotoAdapter.kt
class PhotoAdapter(val context: Context, val postsList: ArrayList<Posts>) :
RecyclerView.Adapter<PhotoAdapter.PhotoViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PhotoViewHolder {
val view = LayoutInflater.from(parent.context).inflate(R.layout.post_layout, parent, false)
return PhotoViewHolder(view)
}
override fun onBindViewHolder(holder: PhotoViewHolder, position: Int) {
val currentItem = postsList[position]
holder.descText.text = currentItem.description
Glide.with(context).load(currentItem.imageUri).into(holder.postImageView)
holder.dateText.text = currentItem.postDate
}
override fun getItemCount(): Int {
return postsList.size
}
class PhotoViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val postImageView: ImageView = itemView.findViewById(R.id.postImages)
val dateText: TextView = itemView.findViewById(R.id.textDate)
val descText: TextView = itemView.findViewById(R.id.textDesc)
}
}
云 Firestore 结构的示例图像
截图中的imageUri
值以content://
开头,表示指向特定设备上的特定文件。如果您与其他用户共享 URL,该文件将不存在于他们的设备上 - 因此它没有任何内容可显示。
您要做的是upload the file from the source device to a cloud storage location, generate a download URL上传的文件,然后将下载的URL存储在数据库中。