将图库图像加载到对话框的图像视图中

时间:2014-09-13 18:49:23

标签: android android-dialog

我需要将图片加载到自定义对话框(扩展对话框的类)上的图像视图中。

  1. 在类中调用对话框,扩展对话框,让用户选择从图库加载图片或从相机拍照...到目前为止一切都很好!

  2. 然后startActivityForResult()不是Dialog对象的一部分! 并获得结果

  3. onActivityResult()也不属于Dialog类!

  4. 代码:

        package ....;
    
        .....
    
        public class AnosLeitura extends Dialog {
    
            ...
    
            public void btnImageLogo_OnClick(View v) {
                edLogo.setImageResource(R.drawable.no_image);
                final String[] option = new String[] { "From camera", 
                                                       "From gallery" };
                ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
                                               android.R.layout.select_dialog_item, option);
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("Choice");
                builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {
                        if (which == 0) { callCamera(); }
                        if (which == 1) { callGallery(); }
                    }
                });
                final AlertDialog dialog = builder.create();
                dialog.show();
            }
    
            ....
    
            public void callGallery() {
                Intent intent = new Intent();
                intent.setType("image/*");
                intent.setAction(Intent.ACTION_GET_CONTENT);
                intent.putExtra("crop", "true");
                intent.putExtra("aspectX", 0);
                intent.putExtra("aspectY", 0);
                intent.putExtra("outputX", 200);
                intent.putExtra("outputY", 150);
                intent.putExtra("return-data", true);
    
                //This is not possible!
                startActivityForResult(Intent.createChooser(intent, "Complete action using"),
                                                  PICK_FROM_GALLERY);
            }
    
            // Neither this!
            @Override
            protected void onActivityResult(int requestCode, int resultCode, Intent data) {
                if (resultCode != RESULT_OK)
                    return;
    
                switch (requestCode) {
                    case CAMERA_REQUEST: 
                         Bundle extras = data.getExtras();
                         if (extras != null) {
                              Bitmap yourImage = extras.getParcelable("data");
                              ByteArrayOutputStream stream = new ByteArrayOutputStream();
                              yourImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
                              imageInByte = stream.toByteArray();
                              edLogo.setImageBitmap(yourImage); 
                         }
                         break;
                    case PICK_FROM_GALLERY: 
                         Bundle extras2 = data.getExtras();
                         if (extras2 != null) {
                              Bitmap yourImage = extras2.getParcelable("data");
                              ByteArrayOutputStream stream = new ByteArrayOutputStream();
                              yourImage.compress(Bitmap.CompressFormat.PNG, 100, stream);
                              imageInByte = stream.toByteArray();
                              edLogo.setImageBitmap(yourImage); 
                         }
                         break;
                }
         }
    
        ....
    
    }
    

    有谁知道一些解决方案?

    韩国社交协会

0 个答案:

没有答案