将图像转到AlertDialog的问题

时间:2013-06-27 12:57:14

标签: android android-intent imageview android-alertdialog

我有一些代码可以从相机库中获取图片。当你使用活动中的按钮来获取图片并将图片放在该活动上时,它可以正常工作但是当我在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上测试了另一个活动的代码并且它工作正常但是当我打开alertDialog来执行此操作时,我无法将图片恢复到我的imageview。我仍然可以访问我的图库并点击图片,但是当我尝试保存它时,它不会将图片放在图像视图中。

1 个答案:

答案 0 :(得分:2)

尝试将点击侦听器之外的方法onActivityResult与其他活动方法一起移动,如此

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);
                                    }
                                }
     }
}