【发布时间】:2018-09-17 20:18:33
【问题描述】:
我正在开发一个使用 kotlin 将 pdf 文件上传到 firebase 存储的 android 应用程序
我遵循了一个教程,在运行时我只能浏览我的文件,但我无法选择要上传的任何文件这里是截图screenshot
这是来自 mainActivity.kt 的代码
在 MainActivity 类下:AppCompatActivity()
val pdf: Int=0
lateinit var uri:Uri
lateinit var mStorage: StorageReference
在 onCreate() 下
val pdfBtn=findViewById<Button>(R.id.pdfBtn)
mStorage=FirebaseStorage.getInstance().getReference("Uploads")
pdfBtn.setOnClickListener(View.OnClickListener {
view: View-> val intent = Intent()
intent.setType("pdf/*")
intent.setAction(Intent.ACTION_GET_CONTENT)
startActivityForResult(Intent.createChooser(intent,"Select PDF"),pdf)
})
其余与上传相关的方法
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
val uriTxt=findViewById<TextView>(R.id.uriTxt)
if(resultCode== Activity.RESULT_OK){
if(requestCode==pdf){
uri=data!!.data
uriTxt.text=uri.toString()
upload()
}
}
super.onActivityResult(requestCode, resultCode, data)
}
private fun upload(){
var mRefrence= mStorage.child(uri.lastPathSegment)
try{
mRefrence.putFile(uri).addOnSuccessListener {
taskSnapshot: UploadTask.TaskSnapshot? -> var url =taskSnapshot!!.downloadUrl
val dwnTxt=findViewById<TextView>(R.id.dwnTxt)
dwnTxt.text=url.toString()
Toast.makeText(this,"Successfully uploaded",Toast.LENGTH_LONG).show()
}
}
catch (e: Exception){
Toast.makeText(this,e.toString(),Toast.LENGTH_LONG).show()
}
}
请有人更正我的代码并告诉我它有什么问题。 非常感谢
【问题讨论】:
标签: android firebase kotlin firebase-storage