通过 AlertDialog 获取图像的问题

我在程序中从camera gallery中获取图片。当我使用一个按钮从一个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;
            }
        });

我在另一个 activity 中 使用按钮来测试代码可以正常运行。但是当我打开alertDialog 来实现,我就不能把图像放回 imageview ,我依然可以从 Gallery 获取图像和点击一个图像,但是当我想保存它时,它不能把图像放在 imageview 中。

看起来你在尝试在 AlertDialog 中调用 startActivityForResult() 方法来从图库中选择图片并将其加载到 ImageView 中。然而, startActivityForResult() 方法需要被调用的 Activity 对象来执行。因此,当你尝试在 AlertDialog 的内部类中调用该方法时,它无法正常工作。


在这种情况下,最简单的解决方案是将你的图库选择代码放到你的 Activity 中,然后在 AlertDialog 中调用它。例如,你可以在 Activity 的 onCreate() 方法中设置图库选择的按钮监听器,然后在 AlertDialog 中调用监听器来打开图库:

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

// ...

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)).performClick();
        alertDialog.show();
        return true;
    }
});