import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatactivity;
import de.hdodenhof.circleimageview.CircleImageView;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.Datasnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.google.firebase.storage.FirebaseStorage;
import com.google.firebase.storage.StorageReference;
import com.google.firebase.storage.UploadTask;
import com.theartofdev.edmodo.cropper.CropImage;
import com.theartofdev.edmodo.cropper.CropImageView;
import java.util.HashMap;
public class Settingsactivity extends AppCompatactivity {
private Button UpdateaccountSettings;
private EditText username,userStatus;
private CircleImageView userProfileImage;
private ProgressDialog loadingBar;
private String currentUserID;
private FirebaseAuth mAuth;
private DatabaseReference RootRef;
private final static int GalleryPick = 1;
private StorageReference UserProfileImagesRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
mAuth = FirebaseAuth.getInstance();
currentUserID = mAuth.getcurrentUser().getUid();
RootRef = FirebaseDatabase.getInstance().getReference();
UserProfileImagesRef = FirebaseStorage.getInstance().getReference().child("Profile Images");
InitializeFields();
username.setVisibility(View.INVISIBLE);
UpdateaccountSettings.setOnClicklistener(new View.OnClicklistener() {
@Override
public void onClick(View view) {
UpdateSettings();
}
});
RetrieveUserInfo();
userProfileImage.setOnClicklistener(new View.OnClicklistener() {
@Override
public void onClick(View view) {
Intent galleryIntent = new Intent();
galleryIntent.setaction(Intent.actION_GET_CONTENT);
galleryIntent.setType("image/*");
startactivityForResult(galleryIntent,GalleryPick);
}
});
}
private void InitializeFields() {
UpdateaccountSettings = (Button) findViewById(R.id.update_settings_button);
username = (EditText) findViewById(R.id.set_user_name);
userStatus = (EditText) findViewById(R.id.set_profile_status);
userProfileImage = (CircleImageView) findViewById(R.id.set_profile_image);
loadingBar = new ProgressDialog(this);
}
@Override
protected void onactivityResult(int requestCode,int resultCode,@Nullable Intent data) {
super.onactivityResult(requestCode,resultCode,data);
if (requestCode == GalleryPick && resultCode == RESULT_OK && data != null) {
Uri ImageUri = data.getData();
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.setaspectRatio(1,1)
.start(this);
}
if (requestCode == CropImage.CROP_IMAGE_actIVITY_REQUEST_CODE)
{
CropImage.activityResult result = CropImage.getactivityResult(data);
if (resultCode == RESULT_OK)
{
loadingBar.setTitle("Set Profile Image");
loadingBar.setMessage("Please wait,your profile image is updating...");
loadingBar.setCanceledOnTouchOutside(false);
loadingBar.show();
Uri resulturi = result.geturi();
StorageReference filePath = UserProfileImagesRef.child(currentUserID + ".jpg");
filePath.putFile(resulturi).addOnCompleteListener(new OnCompleteListener<UploadTask.Tasksnapshot>()
{
@Override
public void onComplete(@NonNull Task<UploadTask.Tasksnapshot> task)
{
if (task.isSuccessful())
{
Toast.makeText(Settingsactivity.this,"Profile Image uploaded Successfully...",Toast.LENGTH_SHORT).show();
} else
{
String message = task.getException().toString();
Toast.makeText(Settingsactivity.this,"Error: " + message,Toast.LENGTH_SHORT).show();
}
}
});
}
}
}
private void UpdateSettings ()
{
String setusername = username.getText().toString();
String setStatus = userStatus.getText().toString();
if (TextUtils.isEmpty(setusername)) {
Toast.makeText(this,"Please write your user name first....",Toast.LENGTH_SHORT).show();
}
if (TextUtils.isEmpty(setStatus)) {
Toast.makeText(this,"Please write your status....",Toast.LENGTH_SHORT).show();
} else {
HashMap<String,Object> profileMap = new HashMap<>();
profileMap.put("uid",currentUserID);
profileMap.put("name",setusername);
profileMap.put("status",setStatus);
RootRef.child("Users").child(currentUserID).setvalue(profileMap)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
SendUserToMainactivity();
Toast.makeText(Settingsactivity.this,"Profile Updated Successfully...",Toast.LENGTH_SHORT).show();
String downloadUrl = task.getResult().getDownloadUrl().toString();
RootRef.child("Users").child(currentUserID).child("image")
.setvalue(downloadUrl)
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful())
{
Toast.makeText(Settingsactivity.this,"Image save in Database,Successfully...",Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}
else
{
String message = task.getException().toString();
Toast.makeText(Settingsactivity.this,Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}
}
});
}
else
{
String message = task.getException().toString();
Toast.makeText(Settingsactivity.this,Toast.LENGTH_SHORT).show();
loadingBar.dismiss();
}
}
});
}
}
private void RetrieveUserInfo () {
RootRef.child("Users").child(currentUserID)
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull Datasnapshot datasnapshot) {
if ((datasnapshot.exists()) && (datasnapshot.hasChild("name") && (datasnapshot.hasChild("image")))) {
String retrieveusername = datasnapshot.child("name").getvalue().toString();
String retrievesStatus = datasnapshot.child("status").getvalue().toString();
String retrieveProfileImage = datasnapshot.child("image").getvalue().toString();
username.setText(retrieveusername);
userStatus.setText(retrievesStatus);
} else if ((datasnapshot.exists()) && (datasnapshot.hasChild("name"))) {
String retrieveusername = datasnapshot.child("name").getvalue().toString();
String retrievesStatus = datasnapshot.child("status").getvalue().toString();
username.setText(retrieveusername);
userStatus.setText(retrievesStatus);
} else {
username.setVisibility(View.VISIBLE);
Toast.makeText(Settingsactivity.this,"Please set & update your profile information...",Toast.LENGTH_SHORT).show();
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
private void SendUserToMainactivity ()
{
Intent mainIntent = new Intent(Settingsactivity.this,Mainactivity.class);
mainIntent.addflags(Intent.flaG_actIVITY_NEW_TASK | Intent.flaG_actIVITY_CLEAR_TASK);
startactivity(mainIntent);
finish();
}
}
无法解决此问题(字符串downloadUrl = task.getResult()。getDownloadUrl()。toString();),任何人都无法详细解释下一步该怎么做。搜索解决方案至少3个小时,但仍然无法获取。 尝试更改Tasksnapshot,但现在我对自己编写代码的代码感到困惑。我一直在遵循youtube指南,到3年前为止,到目前为止仍然不知何故。