我已创建存储引用并将其描述为文件,我想更改它的名称。但存储引用没有与我的目标相关的方法。
答案 0 :(得分:2)
您可以像这样更改图片名称:
storage = FirebaseStorage.getInstance().getReference();
Long tsLong = System.currentTimeMillis() / 1000;
String ts = tsLong.toString();
StorageReference storageReference = storage.child("image").child(ts + ".jpg");
答案 1 :(得分:1)
我这样做是为了用时间和日期来重命名文件。
Log.d(TAG, "uploadFromUri:src:" + fileUri.toString());
String time = Calendar.getInstance().getTime().toString();
Log.d(TAG, "uploadFromUri: time " + time);
storageRef = storage.getReference().child("/dir/"+firebaseUser.getUid()+"/"+time+"/");
UploadTask uploadTask = storageRef.putFile(fileUri);
答案 2 :(得分:-1)
只需使用此代码..我发现它对我有用。这是kotlin代码。
类MainActivity:AppCompatActivity(){
val PDF : Int = 0
val DOCX : Int = 1
val PPT : Int = 2
lateinit var uri : Uri
lateinit var mStorage : StorageReference
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val pdfBtn = findViewById<View>(R.id.pdfBtn) as Button
val docxBtn = findViewById<View>(R.id.docxBtn) as Button
val pptBtn = findViewById<View>(R.id.pptBtn) as Button
mStorage = FirebaseStorage.getInstance().getReference("Uploads")
pdfBtn.setOnClickListener(View.OnClickListener {
view: View? -> val intent = Intent()
intent.setType ("*/*")
intent.setAction(Intent.ACTION_GET_CONTENT)
startActivityForResult(Intent.createChooser(intent, "Select PDF"), PDF)
})
docxBtn.setOnClickListener(View.OnClickListener {
view: View? -> val intent = Intent()
intent.setType ("*/*")
intent.setAction(Intent.ACTION_GET_CONTENT)
startActivityForResult(Intent.createChooser(intent, "Select DOCX"), DOCX)
})
pptBtn.setOnClickListener(View.OnClickListener {
view: View? -> val intent = Intent()
intent.setType ("*/*")
intent.setAction(Intent.ACTION_GET_CONTENT)
startActivityForResult(Intent.createChooser(intent, "Select PPT"), PPT)
})
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
val uriTxt = findViewById<View>(R.id.uriTxt) as TextView
if (resultCode == RESULT_OK) {
if (requestCode == PDF) {
uri = data!!.data
uriTxt.text = uri.toString()
upload ()
}else if (requestCode == DOCX) {
uri = data!!.data
uriTxt.text = uri.toString()
upload ()
}
else if (requestCode == PPT) {
uri = data!!.data
uriTxt.text = uri.toString()
upload ()
}
}
super.onActivityResult(requestCode, resultCode, data)
}
private fun upload() {
var mReference = mStorage.child(uri.lastPathSegment)
try {
mReference.putFile(uri).addOnSuccessListener {
taskSnapshot: UploadTask.TaskSnapshot? -> var url = taskSnapshot!!.downloadUrl
val dwnTxt = findViewById<View>(R.id.dwnTxt) as TextView
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()
}
}