Я выполняю операции загрузки и выгрузки изображений в приложении. Я использую getUploadSessionUrl()
. Я пытаюсь измениться, но это меня беспокоит. getDownload()
Я проверил базу данных firebase и успешно загруженные файлы.
public class SetupActivity extends AppCompatActivity {
private CircleImageView setupImage;
private Uri mainImageURI = null;
private String user_id;
private boolean isChanged = false;
private EditText setupName;
private Button setupBtn;
private ProgressBar setupProgress;
private StorageReference storageReference;
private FirebaseAuth firebaseAuth;
private FirebaseFirestore firebaseFirestore;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_setup);
Toolbar setupToolbar = findViewById(R.id.setupToolbar);
setSupportActionBar(setupToolbar);
getSupportActionBar().setTitle("Hesap Ayarları");
firebaseAuth = FirebaseAuth.getInstance();
user_id = firebaseAuth.getCurrentUser().getUid();
firebaseFirestore = FirebaseFirestore.getInstance();
storageReference = FirebaseStorage.getInstance().getReference();
setupImage = findViewById(R.id.setup_image);
setupName = findViewById(R.id.setup_name);
setupBtn = findViewById(R.id.setup_btn);
setupProgress = findViewById(R.id.setup_progress);
setupProgress.setVisibility(View.VISIBLE);
setupBtn.setEnabled(false);
firebaseFirestore.collection("Kullanıcılar").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("isim");
String image = task.getResult().getString("image");
mainImageURI = Uri.parse(image);
setupName.setText(name);
/*RequestOptions placeholderRequest = new RequestOptions();
placeholderRequest.placeholder(R.mipmap.setup_profile);*/
RequestOptions placeholderRequest = new RequestOptions();
placeholderRequest.placeholder(R.mipmap.setup_profile);
Glide.with(SetupActivity.this).setDefaultRequestOptions(placeholderRequest).load(image).into(setupImage);
}
} else {
String error = task.getException().getMessage();
Toast.makeText(SetupActivity.this, "(Veritabanı Hatası Retrieve): " + error, Toast.LENGTH_LONG).show();
}
setupProgress.setVisibility(View.INVISIBLE);
setupBtn.setEnabled(true);
}
});
setupBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
final String user_name = setupName.getText().toString();
if(isChanged) {
if (!TextUtils.isEmpty(user_name) && mainImageURI != null) {
user_id = firebaseAuth.getCurrentUser().getUid();
StorageReference image_path = storageReference.child("profile_images").child(user_id + ".jpg");
image_path.putFile(mainImageURI).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(SetupActivity.this, "(Resim Hatası): " + error, Toast.LENGTH_LONG).show();
}
setupProgress.setVisibility(View.INVISIBLE);
}
});
}
} else{
storeFireStore(null,user_name);
}
}
});
setupImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (ContextCompat.checkSelfPermission(SetupActivity.this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(SetupActivity.this, "Telefon Ayarları > Uygulamalar bölümünden, uygulamamıza tüm izinleri vermelisiniz.", Toast.LENGTH_LONG).show();
ActivityCompat.requestPermissions(SetupActivity.this, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, 1);
} else {
BringImagePicker();
}
} else {
BringImagePicker();
}
}
});
}
private void storeFireStore(@NonNull Task<UploadTask.TaskSnapshot> task, String user_name) {
Uri download_uri;
if(task != null) {
download_uri = task.getResult().getUploadSessionUri();
}else{
download_uri = mainImageURI;
}
Map<String, String> userMap = new HashMap<>();
userMap.put("isim", user_name);
userMap.put("image", download_uri.toString());
firebaseFirestore.collection("Kullanıcılar").document(user_id).set(userMap).addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
if (task.isSuccessful()) {
Toast.makeText(SetupActivity.this, "Ayarlar güncellendi ", Toast.LENGTH_LONG).show();
Intent mainIntent = new Intent(SetupActivity.this, MainActivity.class);
startActivity(mainIntent);
finish();
} else {
String error = task.getException().getMessage();
Toast.makeText(SetupActivity.this, "(Veritabanı Hatası): " + error, Toast.LENGTH_LONG).show();
}
}
});
}
private void BringImagePicker() {
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.setAspectRatio(1, 1)
.start(SetupActivity.this);
}
@Override
protected 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();
}
}
}
}