【发布时间】:2021-12-27 22:46:36
【问题描述】:
公共类 SignUpActivity 扩展 AppCompatActivity {
private CircleImageView profilePic,galleryPick,cameraPick;
private ActivityResultLauncher<Intent> activityResultLauncher;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
profilePic = findViewById(R.id.circular_image);
activityResultLauncher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
@Override
public void onActivityResult(ActivityResult result)
{
if(result.getResultCode() == RESULT_OK && result.getData() != null)
{
Bundle bundle = result.getData().getExtras();
Bitmap bitmap = (Bitmap) bundle.get("data");
profilePic.setImageBitmap(bitmap);
}
}
});
profilePic.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
chooseProfilePic();
}
});
}
private void chooseProfilePic()
{
AlertDialog.Builder builder = new AlertDialog.Builder(SignUpActivity.this);
LayoutInflater inflater = getLayoutInflater();
View dialogView = inflater.inflate(R.layout.alert_dialog,null);
builder.setCancelable(false);
builder.setView(dialogView);
galleryPick = findViewById(R.id.gallery_pick);
cameraPick = findViewById(R.id.camera_pick);
AlertDialog alertDialog = builder.create();
alertDialog.show();
cameraPick.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(cameraIntent.resolveActivity(getPackageManager()) != null)
{
activityResultLauncher.launch(cameraIntent);
}
}
});
}
}
所以我是 Android Studio 的初学者,因为 startForActivityResult 已被弃用,所以我是 寻找东西来代替它。新方法有效,但出现问题时 与包括警报对话框在内的私有方法一起使用。
【问题讨论】:
-
请分享您遇到的 logcat 或错误。
标签: java android android-studio android-intent android-alertdialog