用户点击图片后,图库会再次打开

时间:2019-01-24 09:44:35

标签: android android-intent android-gallery

我正在使用下面的代码来激发意图并打开android Gallery,以便用户选择图片。

 userProfileImage.setOnClickListener(v ->
        {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_GET_CONTENT);
            intent.setType("image/*");
            startActivityForResult(intent, GalleryPick);
        });

图库可以很好地打开,但是当用户单击图像时,应用程序显示“选择源”,所有可以打开图像的应用程序。如果用户单击其中之一,则他们必须再次选择图像,然后图像才能继续。得到结果后,下面的代码显示了我的代码:

@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...");
                loadingBar.setCanceledOnTouchOutside(false);
                loadingBar.show();

                assert result != null;
                final Uri resultUri = result.getUri();
                final StorageReference filePath = userProfileImagesRef.child(currentUserID + ".jpg");
                filePath.putFile(resultUri).addOnSuccessListener(taskSnapshot -> filePath.getDownloadUrl().addOnSuccessListener(uri -> {
                    final String downloadUrl = uri.toString();
                    rootRef.child("Users").child(currentUserID).child("image").setValue(downloadUrl)
                            .addOnCompleteListener(task ->
                            {
                                if (task.isSuccessful())
                                {
                                    Toast.makeText(SettingsActivity.this, "Profile image stored to firebase database successfully.", Toast.LENGTH_SHORT).show();
                                    loadingBar.dismiss();
                                }
                                else
                                    {
                                        String message = Objects.requireNonNull(task.getException()).getMessage();
                                        Toast.makeText(SettingsActivity.this, "Error Occurred..." + message, Toast.LENGTH_SHORT).show();
                                        loadingBar.dismiss();
                                    }
                            });
                }));
            }
            else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE)
            {
                assert result != null;
                Exception error = result.getError();
            }

        }
    }

有人可以告诉我我在做什么错以及如何解决此问题吗?我希望让用户选择他们要用来打开图片的应用。

0 个答案:

没有答案