cheli_71 2013-08-02 02:55 采纳率: 0%
浏览 2034

通过 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 中。

  • 写回答

1条回答 默认 最新

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2023-01-09 20:35
    关注

    看起来你在尝试在 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;
        }
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler