我这里有一个配置文件设置代码,可以上传和检索数据。上传有效,但数据检索无效。
我搜索了与此有关的另一个主题,但没有成功,请帮我寻找我好几天了
private CircleImageView setupImage;
private Uri mainImageUri = null;
private EditText setupName;
private Button setupBtn;
private StorageReference storageReference;
private FirebaseAuth firebaseAuth;
private ProgressDialog progressDialog;
private FirebaseFirestore firebaseFirestore;
private String user_id;
private boolean isChanged = false;
Bitmap compressedImageFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_account_settings);
Toolbar setupToolbar = findViewById(R.id.setupToolbar);
setSupportActionBar(setupToolbar);
getSupportActionBar().setTitle("Account Settings");
firebaseAuth = FirebaseAuth.getInstance();
storageReference = FirebaseStorage.getInstance().getReference();
user_id = firebaseAuth.getCurrentUser().getUid();
setupName = findViewById(R.id.setup_name);
setupBtn = findViewById(R.id.setup_btn);
setupImage = findViewById(R.id.setupImage);
progressDialog = new ProgressDialog(this);
firebaseFirestore = FirebaseFirestore.getInstance();
firebaseFirestore.collection("Users").document("user_id").get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
@Override
public void onComplete(@NonNull Task<DocumentSnapshot> task) {
if(task.isSuccessful()){
if(task.getResult().exists()){
String name = task.getResult().getString("name");
String image = task.getResult().getString("image");
mainImageUri = Uri.parse(image);
setupName.setText(name);
RequestOptions placeholderRequest = new RequestOptions();
placeholderRequest.placeholder(R.drawable.accountprofile);
Glide.with(AccountSettings.this).setDefaultRequestOptions(placeholderRequest).load(image).into(setupImage);
}
}
else{
String error = task.getException().getMessage();
Toast.makeText(AccountSettings.this,"Firestore Retrieval Error!"+error,Toast.LENGTH_LONG).show();
}
}
});
setupBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
progressDialog.setMessage("Loading please wait...");
progressDialog.show();
final String user_name = setupName.getText().toString();
if(isChanged) {
String user_id = firebaseAuth.getCurrentUser().getUid();
File newImageFile = new File(mainImageUri.getPath());
try {
compressedImageFile = new Compressor(AccountSettings.this)
.setMaxHeight(125)
.setMaxWidth(125)
.setQuality(50)
.compressToBitmap(newImageFile);
} catch (IOException e) {
e.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
compressedImageFile.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] thumbData = baos.toByteArray();
if (!TextUtils.isEmpty(user_name) && mainImageUri != null) {
UploadTask image_path = storageReference.child("profile_images").child(user_id + ".jpg").putBytes(thumbData);
image_path.addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful()) {
storeFirestore(task, user_name);
} else {
String error = task.getException().getMessage();
Toast.makeText(AccountSettings.this, "Image Error: " + error, Toast.LENGTH_SHORT).show();
}
progressDialog.dismiss();
}
});
}
}
else{
storeFirestore(null,user_name);
}
}
});
setupImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if(ContextCompat.checkSelfPermission(AccountSettings.this, Manifest.permission.READ_EXTERNAL_STORAGE)!= PackageManager.PERMISSION_GRANTED){
Toast.makeText(AccountSettings.this, "Permission Denied!",Toast.LENGTH_LONG).show();
ActivityCompat.requestPermissions(AccountSettings.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1);
} else {
BringImagePicker();
}
}
else{
BringImagePicker();
}
}
});
}
private void storeFirestore( Task<UploadTask.TaskSnapshot> task,String user_name) {
String download_uri;
if(task != null){
download_uri = task.getResult().getMetadata().getReference().getDownloadUrl().toString();
}
else {
download_uri = mainImageUri.toString();
}
Map<String, Object> user_map = new HashMap<>();
user_map.put("name", user_name);
user_map.put("image", download_uri);
firebaseFirestore.collection("Users").document(user_id).set(user_map).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if(task.isSuccessful()){
Toast.makeText(AccountSettings.this, "The user Settings are updated.", Toast.LENGTH_LONG).show();
startActivity(new Intent(AccountSettings.this, Feed.class));
finish();
}
else{
String error = task.getException().getMessage();
Toast.makeText(AccountSettings.this,"Firestore Error: " +error, Toast.LENGTH_SHORT).show();
}
}
});
}
public void BringImagePicker(){
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1,1)
.start(AccountSettings.this);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode,resultCode,data);
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
mainImageUri = result.getUri();
setupImage.setImageURI(mainImageUri);
isChanged = true;
} else if(resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE){
Exception error = result.getError();
}
}
}
}
请帮助我如何处理它。我只想将图像和字符串值上传到Firebase Firestore,当我尝试打开页面时,它将自动检索数据(如果存在)。