如果从图库中选择图像后,如何设置要查看的图像,调用 onCreate 方法并且它已经在该视图中设置了图像?

Posted

技术标签:

【中文标题】如果从图库中选择图像后,如何设置要查看的图像,调用 onCreate 方法并且它已经在该视图中设置了图像?【英文标题】:how to set image to view if after picking it from gallery, onCreate method is called and it alreay has a image to set at that view? 【发布时间】:2021-06-14 15:20:29 【问题描述】:

我试图在从 onActivityResult 中选择图像后将其设置为 CircleImageView。 但问题是,当创建该活动时,它已经在 onCreate 中有一个方法,该方法正在为该视图设置图像。因此,每当我从图库中选择图像并返回到该活动时,oncreate 方法中的图像就会设置为该视图。 我该怎么做才能在活动开始时,首先将 onCreate 方法图像设置为该视图,然后选择图像后,将拾取的图像设置为该视图。

我的代码:

 public class MentorProfileActivity extends AppCompatActivity 


long fileSize;
public static final int REQUEST_IMAGE = 100;

@BindView(R.id.profile_image_mentor)
CircleImageView profileImageMentor;
@BindView(R.id.edit_image_mentor)
CircleImageView editImageMentor;
   ProgressBar pbMentor;
String name, email, userId, password, contact, mentor, imageUrl, bio, category, uriString,
        youtubeLink, facebookLink, instagramLink, twitterLink, extension;
String fileName, path, displayName, imageName;
LoginResponse loginResponse;
String videoViewLink = "https://smakerspace.s3.ap-south-1.amazonaws.com/Video/";
Uri uri, uriSelectedImage, file1;
File myFile;
SharedPreferences sharedPreferences;
ProgressDialog progressDoalog;

boolean isImageInit = false;

@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_mentor_profile);
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
    ButterKnife.bind(this);
    pbMentor.setVisibility(View.GONE);

    if (getIntent().hasExtra("Mentor")) 
        mentor = getIntent().getStringExtra("Mentor");
    
    if (getIntent().hasExtra("MentorPassword")) 
        password = getIntent().getStringExtra("MentorPassword");
    

    sharedPreferences = this.getSharedPreferences("spMentor", Context.MODE_PRIVATE);

    userId = sharedPreferences.getString("userIdMentor", "");

    getUserDetails(userId, password, mentor);






   public void getUserDetails(String userId, String password, String mentor) 
    pbMentor.setVisibility(View.VISIBLE);
    retrofit2.Call<LoginResponse> ourSupplierResponseCall = RestClient.getClient().
            verifyLogin(userId, password, mentor);
    ourSupplierResponseCall.enqueue(new Callback<LoginResponse>() 
        @Override
        public void onResponse(retrofit2.Call<LoginResponse> call, Response<LoginResponse> response) 
            if (response.isSuccessful()) 
                pbMentor.setVisibility(View.GONE);
                loginResponse = response.body();


                if (loginResponse.getError().booleanValue() == false) 


                    imageUrl = "https://smakerspace.s3.ap-south-1.amazonaws.com/upload/" + loginResponse.getUser().getProfilePic();
                    Picasso.get().load(imageUrl).into(profileImageMentor);
                    isImageInit = true;


                 else 

                

             else 
            
        

        @Override
        public void onFailure(Call<LoginResponse> call, Throwable t) 
            pbMentor.setVisibility(View.GONE);
        
    );



@OnClick(R.id.edit_image_mentor)
void onProfileImageClick() 
                if (!isImageInit) 
                Log.d(TAG, "onProfileImageClick:Image is initing, please wait 
                ");
                return;
            
    Dexter.withActivity(this)
            .withPermissions(Manifest.permission.CAMERA, Manifest.permission.WRITE_EXTERNAL_STORAGE)
            .withListener(new MultiplePermissionsListener() 
                @Override
                public void onPermissionsChecked(MultiplePermissionsReport report) 
                    if (report.areAllPermissionsGranted()) 

                        Intent pickIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                        pickIntent.setType("image/*");
                        startActivityForResult(pickIntent, REQUEST_IMAGE);


                    

                    if (report.isAnyPermissionPermanentlyDenied()) 
                        showSettingsDialog();
                    
                

                @Override
                public void onPermissionRationaleShouldBeShown(List<PermissionRequest> permissions, PermissionToken token) 
                    token.continuePermissionRequest();
                
            ).check();


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 

    super.onActivityResult(requestCode, resultCode, data);
    if (resultCode == RESULT_OK && requestCode == REQUEST_IMAGE) 
        uri = data.getData();
        uriString = uri.toString();
    loadProfile(uriString);
    



private void loadProfile(String url) 

    Picasso.get().load(url).into(profileImageMentor);
    profileImageMentor.setColorFilter(ContextCompat.getColor(this, android.R.color.transparent));




private MentorProfileActivity getActivity() 
    return MentorProfileActivity.this;

图像设置几秒钟,然后重新设置上一个图像。

【问题讨论】:

【参考方案1】:

可以尝试在代码中添加布尔标志 isImageInit:

    boolean isImageInit = false;
    public void getUserDetails(String userId, String password, String mentor) 
        pbMentor.setVisibility(View.VISIBLE);
        retrofit2.Call<LoginResponse> ourSupplierResponseCall = RestClient.getClient().
                verifyLogin(userId, password, mentor);
        ourSupplierResponseCall.enqueue(new Callback<LoginResponse>() 
            @Override
            public void onResponse(retrofit2.Call<LoginResponse> call, Response<LoginResponse> response) 
                if (response.isSuccessful()) 
                    pbMentor.setVisibility(View.GONE);
                    loginResponse = response.body();
                    
                    if (loginResponse.getError().booleanValue() == false) 
                        imageUrl = "https://smakerspace.s3.ap-south-1.amazonaws.com/upload/" + loginResponse.getUser().getProfilePic();
                        Picasso.get().load(imageUrl).into(profileImageMentor);
                        isImageInit = true;
                     else 
                 else 
            
            @Override
            public void onFailure(Call<LoginResponse> call, Throwable t) 
                pbMentor.setVisibility(View.GONE);
            
        );
    @OnClick(R.id.edit_image_mentor)
    void onProfileImageClick() 
        if(!isImageInit)
            Log.d(TAG, "onProfileImageClick:Image is initing, please wait ");
            return;
        
        //... your old code
    

【讨论】:

还是同样的问题,选择图像后查看并再次显示响应图像。 建议您在isImageInit 显示网络图片后检查是否允许点击,设置为true,您可以更新修改后的代码。 我已经更新了代码,如果我做错了请检查一下。 @雪莉

以上是关于如果从图库中选择图像后,如何设置要查看的图像,调用 onCreate 方法并且它已经在该视图中设置了图像?的主要内容,如果未能解决你的问题,请参考以下文章

Android:如何在设置图像视图时检测从图库中选择的图像方向(纵向或横向)?

Android:在捕获或从图库中挑选后裁剪图像

从图库中选择图像后如何打开下一个视图控制器(swift)

从图库或相机中选择图像的对话框

使用片段从图库中选择图像

Phonegap - 从图库中选择图像