gpt4 book ai didi

java - Android Preference Image Picker - 如何在 DialogPreference 中接收结果

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:43:01 26 4
gpt4 key购买 nike

我想知道如何从 DialogPreference 中的图像选取器流接收结果.

我希望 DialogPreferenceonActivityResult 之后被调用,这样它就可以使用所选图像的 Uri 位置显示为在点击确定/取消之前,在他们的对话框中向用户预览图像。

也许我需要在 onActivityResult 的末尾设置一些东西,然后在 DialogPreference 中调用一个生命周期钩子(Hook),但我不确定。

目前的逻辑是这样的:

ImagePreference.java

public class ImagePreference extends DialogPreference {

View mView;

public ImagePreference(Context context, AttributeSet attrs) {
super(context, attrs);
initWith(context, attrs);
}

private void initWith(Context context, AttributeSet attrs) {
setWidgetLayoutResource(R.layout.pref_image_widget);
setDialogLayoutResource(R.layout.pref_image_dialog);

}

@Override
protected View onCreateDialogView() {
mView = super.onCreateDialogView();

ImageButton button = (ImageButton) mView.findViewById(R.id.add_image);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
((SettingsContract.SelectImage)getContext()).fromGallery();
}
});

return mView;
}

SettingsActivity.java

public class SettingsActivity extends AppCompatActivity
implements SettingsContract.SelectImage {

private static final int PICK_IMAGE_REQUEST = 1;

// ...

@Override
public void fromGallery() {
Intent intent = new Intent();
// Show only images, no videos or anything else
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// Always show the chooser (if there are multiple options available)
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri uri = data.getData();
// what to do here??
}
}

最佳答案

您不能将 AlertDialogImageView 结合使用吗。

您可以设置一个对话框,如下所示:https://stackoverflow.com/a/2115770/1985387

new AlertDialog.Builder(context)
.setTitle("Title")
.setMessage("Here is a preview") //not necessary, you could remove to just show image
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();

要添加一个 ImageView,您可以在 .show() 之前添加它

.setView(imageView);

您可以像这样从 URI 加载 ImageView https://stackoverflow.com/a/9080762/1985387 :

Uri imgUri = Uri.parse(uri);
imageView.setImageURI(null);
imageView.setImageURI(imgUri);

关于java - Android Preference Image Picker - 如何在 DialogPreference 中接收结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41523620/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com