请帮帮我。请帮我解决这个问题。
错误:未解析的引用下载
C:\Users\mayer\Desktop\Instagramapp\app\src\main\java\com\example\instagramapp\activities\EditProfileActivity.kt:(91,78):未解决的参考资料: downloadUrl
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == TAKE_PICTURE_REQUEST_CODE && resultCode == RESULT_OK) {
val uid = mAuth.currentUser!!.uid
mStorage.child("users/$uid/photo").putFile(mImageUri).addOnCompleteListener {
if (it.isSuccessful) {
mDatabase.child("users/$uid/photo").setValue(it.result!!.downloadUrl.toString())
.addOnCompleteListener {
if (it.isSuccessful) {
Log.d(TAG, "onActivityResult: photo saved successfully")
} else {
showToast(it.exception!!.message!!)
}
}
} else {
showToast(it.exception!!.message!!)
}
}
}
}
EditProfileActivity.kt
class EditProfileActivity : AppCompatActivity(), PasswordDialog.Listener {
private val TAG = "EditProfileActivity"
private lateinit var mUser: User
private lateinit var mPendingUser: User
private lateinit var mAuth: FirebaseAuth
private lateinit var mDatabase: DatabaseReference
private lateinit var mStorage: StorageReference
private val TAKE_PICTURE_REQUEST_CODE = 1
val simpleDateFormat = SimpleDateFormat("yyyyMMdd_HHmmss", Locale.US)
private lateinit var mImageUri: Uri
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_edit_profile)
Log.d(TAG, "onCreate")
close_image.setOnClickListener { finish() }
save_image.setOnClickListener { updateProfile() }
change_photo_text.setOnClickListener { takeCameraPicture() }
mAuth = FirebaseAuth.getInstance()
mDatabase = FirebaseDatabase.getInstance().reference
mStorage = FirebaseStorage.getInstance().reference
mDatabase.child("users").child(mAuth.currentUser!!.uid)
.addListenerForSingleValueEvent(ValueEventListenerAdapter {
mUser = it.getValue(User::class.java)!!
name_input.setText(mUser.name, TextView.BufferType.EDITABLE)
username_input.setText(mUser.username, TextView.BufferType.EDITABLE)
website_input.setText(mUser.website, TextView.BufferType.EDITABLE)
bio_input.setText(mUser.bio, TextView.BufferType.EDITABLE)
email_input.setText(mUser.email, TextView.BufferType.EDITABLE)
phone_input.setText(mUser.phone.toString(), TextView.BufferType.EDITABLE)
})
}
private fun takeCameraPicture() {
val intent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
if (intent.resolveActivity(packageManager) != null) {
val imageFile = createImageFile()
mImageUri = FileProvider.getUriForFile(this,
"com.example.instagramapp.fileprovider",
imageFile)
intent.putExtra(MediaStore.EXTRA_OUTPUT, mImageUri)
startActivityForResult(intent, TAKE_PICTURE_REQUEST_CODE)
}
}
private fun createImageFile(): File {
val storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES)
return File.createTempFile(
"JPEG_${simpleDateFormat.format(Date())}_",
".jpg",
storageDir
)
}
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == TAKE_PICTURE_REQUEST_CODE && resultCode == RESULT_OK) {
val uid = mAuth.currentUser!!.uid
mStorage.child("users/$uid/photo").putFile(mImageUri).addOnCompleteListener {
if (it.isSuccessful) {
mDatabase.child("users/$uid/photo").setValue(it.result!!.downloadUrl.toString())
.addOnCompleteListener {
if (it.isSuccessful) {
Log.d(TAG, "onActivityResult: photo saved successfully")
} else {
showToast(it.exception!!.message!!)
}
}
} else {
showToast(it.exception!!.message!!)
}
}
}
}
private fun updateProfile() {
mPendingUser = readInputs()
val error = validate(mPendingUser)
if (error == null) {
if (mPendingUser.email == mUser.email) {
updateUser(mPendingUser)
} else {
PasswordDialog().show(supportFragmentManager, "password_dialog")
}
} else {
showToast(error)
}
}
private fun readInputs(): User {
val phoneStr = phone_input.text.toString()
return User(
name = name_input.text.toString(),
username = username_input.text.toString(),
website = website_input.text.toString(),
bio = bio_input.text.toString(),
email = email_input.text.toString(),
phone = if (phoneStr.isEmpty()) 0 else phoneStr.toLong()
)
}
override fun onPasswordConfirm(password: String) {
if (password.isNotEmpty()) {
val credential = EmailAuthProvider.getCredential(mUser.email, password)
mAuth.currentUser!!.reauthenticate(credential) {
mAuth.currentUser!!.updateEmail(mPendingUser.email) {
updateUser(mPendingUser)
}
}
} else {
showToast("You should enter your password")
}
}
private fun updateUser(user: User) {
val updatesMap = mutableMapOf<String, Any>()
if (user.name != mUser.name) updatesMap["name"] = user.name
if (user.username != mUser.username) updatesMap["username"] = user.username
if (user.website != mUser.website) updatesMap["website"] = user.website
if (user.bio != mUser.bio) updatesMap["bio"] = user.bio
if (user.email != mUser.email) updatesMap["email"] = user.email
if (user.phone != mUser.phone) updatesMap["phone"] = user.phone
mDatabase.updateUser(mAuth.currentUser!!.uid, updatesMap) {
showToast("Profile saved")
finish()
}
}
private fun validate(user: User): String? =
when {
user.name.isEmpty() -> "Please enter name"
user.username.isEmpty() -> "Please enter username"
user.email.isEmpty() -> "Please enter email"
else -> null
}
private fun DatabaseReference.updateUser(uid: String, updates: Map<String, Any>,
onSuccess: () -> Unit) {
child("users").child(mAuth.currentUser!!.uid).updateChildren(updates)
.addOnCompleteListener {
if (it.isSuccessful) {
onSuccess()
} else {
showToast(it.exception!!.message!!)
}
}
}
private fun FirebaseUser.updateEmail(email: String, onSuccess: () -> Unit) {
updateEmail(email).addOnCompleteListener {
if (it.isSuccessful) {
onSuccess()
} else {
showToast(it.exception!!.message!!)
}
}
}
private fun FirebaseUser.reauthenticate(credential: AuthCredential, onSuccess: () -> Unit) {
reauthenticate(credential).addOnCompleteListener {
if (it.isSuccessful) {
onSuccess()
} else {
showToast(it.exception!!.message!!)
}
}
}
}
activity_edit_profile.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activities.EditProfileActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
style="@style/toolbar"
app:contentInsetStart="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="@+id/close_image"
style="@style/toolbar_image"
android:src="@drawable/close"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
style="@style/toolbar_title"
android:text="Edit Profile"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@id/close_image"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="@+id/save_image"
style="@style/toolbar_image"
android:src="@drawable/check"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.appcompat.widget.Toolbar>
<de.hdodenhof.circleimageview.CircleImageView
android:id="@+id/profile_image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginTop="15dp"
android:src="@drawable/profile"
app:civ_border_color="@color/grey"
app:civ_border_width="1dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/toolbar" />
<TextView
android:id="@+id/change_photo_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Change photo"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/profile_image"
android:layout_marginTop="10dp"
android:textColor="@color/blue"
android:textStyle="bold"/>
<TextView
android:id="@+id/name_label"
style="@style/edit_profile_label"
android:labelFor="@id/name_input"
android:text="Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/change_photo_text" />
<EditText
android:id="@+id/name_input"
style="@style/edit_profile_input"
android:layout_marginEnd="20dp"
android:inputType="text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/name_label" />
<TextView
android:id="@+id/username_label"
style="@style/edit_profile_label"
android:labelFor="@id/username_input"
android:text="Username"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/name_input" />
<EditText
android:id="@+id/username_input"
style="@style/edit_profile_input"
android:layout_marginEnd="20dp"
android:inputType="text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/username_label" />
<TextView
android:id="@+id/website_label"
style="@style/edit_profile_label"
android:labelFor="@id/website_input"
android:text="Website"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/username_input" />
<EditText
android:id="@+id/website_input"
style="@style/edit_profile_input"
android:layout_marginEnd="20dp"
android:inputType="text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/website_label" />
<TextView
android:id="@+id/bio_label"
style="@style/edit_profile_label"
android:labelFor="@id/bio_input"
android:text="Bio"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/website_input" />
<EditText
android:id="@+id/bio_input"
style="@style/edit_profile_input"
android:layout_marginEnd="20dp"
android:inputType="text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/bio_label" />
<TextView
android:id="@+id/private_info_text"
style="@style/edit_profile_label"
android:text="Private Information"
android:textColor="@color/black"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/bio_input" />
<TextView
android:id="@+id/email_label"
style="@style/edit_profile_label"
android:labelFor="@id/email_input"
android:text="Email"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/private_info_text" />
<EditText
android:id="@+id/email_input"
style="@style/edit_profile_input"
android:layout_marginEnd="20dp"
android:inputType="textEmailAddress"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/email_label" />
<TextView
android:id="@+id/phone_label"
style="@style/edit_profile_label"
android:labelFor="@id/phone_input"
android:text="Phone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/email_input" />
<EditText
android:id="@+id/phone_input"
style="@style/edit_profile_input"
android:layout_marginEnd="20dp"
android:inputType="phone"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@id/phone_label" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.instagramapp">
<uses-feature android:name="android.hardware.camera"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".activities.HomeActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".activities.SearchActivity" />
<activity android:name=".activities.ShareActivity" />
<activity android:name=".activities.LikesActivity" />
<activity android:name=".activities.ProfileActivity" />
<activity android:name=".activities.EditProfileActivity" />
<activity
android:name=".activities.LoginActivity"
android:windowSoftInputMode="stateVisible|adjustResize" />
<activity
android:name=".activities.RegisterActivity"
android:windowSoftInputMode="stateVisible|adjustResize"></activity>
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="com.example.instagramapp.fileprovider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths"></meta-data>
</provider>
</application>
</manifest>
build.gradle
apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
android {
compileSdkVersion 29
buildToolsVersion "29.0.2"
defaultConfig {
applicationId "com.example.instagramapp"
minSdkVersion 21
targetSdkVersion 29
versionCode 1
versionName "1.0"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.core:core-ktx:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.github.ittianyu:BottomNavigationViewEx:2.0.4'
implementation "com.android.support:design:29.0.0"
implementation 'de.hdodenhof:circleimageview:3.0.1'
implementation 'com.google.firebase:firebase-analytics:17.2.1'
implementation 'com.google.firebase:firebase-core:17.2.1'
implementation 'com.google.firebase:firebase-auth:19.2.0'
implementation 'com.google.firebase:firebase-database:19.2.0'
implementation 'com.google.firebase:firebase-storage:19.1.0'
implementation 'net.yslibrary.keyboardvisibilityevent:keyboardvisibilityevent:2.1.0'
}
apply plugin: 'com.google.gms.google-services'
发布于 2019-12-16 15:49:31
获取下载URL现在需要对服务器进行异步调用,因此不再作为任务上的属性使用。相反,呼叫
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)
if (requestCode == TAKE_PICTURE_REQUEST_CODE && resultCode == RESULT_OK) {
val uid = mAuth.currentUser!!.uid
mStorage.child("users/$uid/photo").putFile(mImageUri).addOnCompleteListener {
if (it.isSuccessful) {
mStorage.child("users/$uid/photo").downloadUrl.addOnCompleteListener{
task -> if(task.isSuccessful){
mDatabase.child("users/$uid/photo").setValue(task.result)
.addOnCompleteListener {
if (it.isSuccessful) {
Log.d(TAG, "onActivityResult: photo saved successfully")
} else {
showToast(it.exception!!.message!!)
}
}
} else {
showToast(it.exception!!.message!!)
}
}
}
}
另见:
发布于 2019-12-16 13:55:26
不推荐使用result.downloadUrl,现在用这种方式使用它
if (it.isSuccessful) {
mDatabase.child("users/$uid/photo").setValue(it.result!!.toString())
.addOnCompleteListener {
if (it.isSuccessful) {
Log.d(TAG, "onActivityResult: photo saved successfully")
} else {
showToast(it.exception!!.message!!)
}
}
} else {
showToast(it.exception!!.message!!)
}
要获得更多信息,请看这个"不建议使用taskSnapshot.getDownloadUrl()“
https://stackoverflow.com/questions/59356681
复制相似问题