There is no error and no problem with clicking the button, but the voice file does not upload to the Firestore. Audio selection is possible, but the message "Successfully Uploaded:" is not output, and the voice file is not uploaded to the Fire Store. I think the cancellation line in onActivityResult and startActivityForResult is the problem. How do I get rid of the cancellation line? And is there any other reason why the file doesn't go up on the fire store?
I modified the Firestore rules.
MainActivity.kt
import ...
class MainActivity : AppCompatActivity() {
val AUDIO : Int = 0
lateinit var uri: Uri
lateinit var mStorage: StorageReference
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val musicBtn = findViewById<View>(R.id.musicBtn) as Button
mStorage = FirebaseStorage.getInstance().getReference("Uploads")
musicBtn.setOnClickListener(View.OnClickListener {
view -> val intent = Intent()
intent.setType("audio/*")
intent.setAction(Intent.ACTION_GET_CONTENT)
startActivityForResult(Intent.createChooser(intent, "Select MP3"), AUDIO)
})
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
val uriTxt = findViewById<View>(R.id.uriTxt)as TextView
if (requestCode== RESULT_OK){
if (requestCode == AUDIO) {
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!!
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()
}
}
}
A cancellation line is created as shown in the image. How do I solve it?
CodePudding user response:
startActivityResult
and onActivityResult
have been deprecated. You need to migrate to registerForActivityResult
and ActivityResultContracts
methods.
A few of the examples are given below:
- https://developer.android.com/codelabs/android-app-permissions
- Predefined Contracts - https://developer.android.com/reference/kotlin/androidx/activity/result/contract/ActivityResultContracts
- https://developer.android.com/reference/kotlin/androidx/activity/result/contract/ActivityResultContract
- https://developer.android.com/training/basics/intents/result
Another easier way you can do this is by ignoring the error and using @Deprecated("Deprecated in Java")
annotation but it is recommended that you migrate.
CodePudding user response:
The cancellation line means those functions are deprecated, but in this case, that is not your problem because they still behave as they used to.
This might be your issue. You have a typo here:
if (requestCode== RESULT_OK){
if (requestCode == AUDIO) {
You should be checking if the resultCode is RESULT_OK
, not the requestCode. So currently your if statement will not run unless your AUDIO
request code happens to be the same as Activity.RESULT_OK
.
If that still doesn't solve it, you should add an OnFailureListener to find out what's happening. You can put a debug breakpoint in your OnFailureListener so you can inspect the error object for what the problem is.