gpt4 book ai didi

android - 让图像通过 AlertDialog 时出现问题

转载 作者:行者123 更新时间:2023-11-29 21:40:16 24 4
gpt4 key购买 nike

我有一些代码可以从相机库中获取照片。当您使用 Activity 中的按钮获取图片并将图片放在该 Activity 上时,它工作正常,但是当我尝试使用 AlertDialog 上的按钮时,我无法将它放到我的 Imageview 中。有帮助吗?

这段代码本身没问题

    ((Button)dialogView.findViewById(R.id.button3))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent,"Select Picture"),SELECT_PICTURE);}
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
im1.setImageURI(selectedImageUri);}}}
public String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);}});

但是当您将此代码添加到 AlertDialog 时,它将不再起作用

        lay1.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
LayoutInflater myLayout = LayoutInflater.from(context);
final View dialogView = myLayout.inflate(R.layout.alerthelp, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
alertDialogBuilder.setView(dialogView);
final AlertDialog alertDialog = alertDialogBuilder.create();


((Button) dialogView.findViewById(R.id.button3))
.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);
}

public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
im1.setImageURI(selectedImageUri);
}
}
}

public String getPath(Uri uri) {
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
}
});
alertDialog.show();
return true;
}
});

我已经从 Button 测试了另一个 Activity 的代码,它工作正常,但是当我打开 alertDialog 来执行它时,我无法将图片返回到我的 imageview。我仍然可以进入我的图库并单击图片,但是当我尝试保存它时,它不会将图片放入 imageView。

最佳答案

尝试将方法 onActivityResult 与其他 Activity 方法一起移到点击监听器之外,就像这样

public class MyActivity extends Activity {

// other methods and fields

private ImageView im1;

// initialize im1 in your onCreate() method

public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
if (requestCode == SELECT_PICTURE) {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
System.out.println("Image Path : " + selectedImagePath);
im1.setImageURI(selectedImageUri);
}
}
}
}

关于android - 让图像通过 AlertDialog 时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17343819/

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