Когда я пытаюсь запустить это приложение, оно открывает галерею, но когда нажимается какая-либо фотография, она не переходит дальше, а снова открывает основное действие. Даже LogCat теперь что-то показывает. Пожалуйста, помогите мне, предложив способ, которым я могу запустить этот код и что на самом деле не так в нем. Это код:
private DatabaseReference mUserDatabase;
private FirebaseUser mCurrentUser;
//Android Layout
private CircleImageView mDisplayImage;
private TextView mName;
//Storage Firebase
private StorageReference mImageStorage;
private static final int IMAGE_REQUEST = 1;
public Uri imageUri;
private StorageTask uploadTask;
private ProgressDialog mProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
mDisplayImage = (CircleImageView) findViewById(R.id.settings_profile);
mName = (TextView) findViewById(R.id.settings_name);
mImageStorage = FirebaseStorage.getInstance().getReference();
mCurrentUser = FirebaseAuth.getInstance().getCurrentUser();
mUserDatabase = FirebaseDatabase.getInstance().getReference().child("Users").child(mCurrentUser.getUid());
mUserDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String name = dataSnapshot.child("name").getValue().toString();
String image = dataSnapshot.child("image").getValue().toString();
String status = dataSnapshot.child("status").getValue().toString();
String thumb_image = dataSnapshot.child("thumb_image").getValue().toString();
mName.setText(name);
mStatus.setText(status);
Picasso.with(SettingsActivity.this).load(image).into(mDisplayImage);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
mDisplayImage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openImage();
}
});
}
private void openImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent, IMAGE_REQUEST);
}
private String getFileExtension (Uri uri) {
ContentResolver contentResolver = getApplicationContext().getContentResolver();
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
return mimeTypeMap.getExtensionFromMimeType(contentResolver.getType(uri));
}
private void uploadImage () {
final ProgressDialog pd = new ProgressDialog(getApplicationContext());
pd.setTitle("Uploading");
pd.setMessage("Please wait while we are processing and uploading the image.");
pd.setCanceledOnTouchOutside(false);
pd.show();
if (imageUri != null) {
final StorageReference fileReference = mImageStorage.child(System.currentTimeMillis() +"."+getFileExtension(imageUri));
uploadTask = fileReference.putFile(imageUri);
uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()) {
throw task.getException();
}
return fileReference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
String mUri = downloadUri.toString();
mUserDatabase = FirebaseDatabase.getInstance().getReference("Users").child(mCurrentUser.getUid());
HashMap<String, Object> map = new HashMap<>();
map.put("image", mUri);
mUserDatabase.updateChildren(map);
pd.dismiss();
}else {
Toast.makeText(SettingsActivity.this, "Failed", Toast.LENGTH_SHORT).show();
pd.dismiss();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(SettingsActivity.this, e.getMessage(), Toast.LENGTH_SHORT).show();
pd.dismiss();
}
});
}else {
Toast.makeText(this, "No Image Selected", Toast.LENGTH_SHORT).show();
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
imageUri = data.getData();
if (uploadTask != null && uploadTask.isInProgress()) {
Toast.makeText(this, "Upload in Progress", Toast.LENGTH_SHORT).show();
}else {
uploadImage();
}