Android完成动作使用

时间:2011-10-10 08:00:25

标签: android

嗨,我有这个按钮,当我点击它时,我想启动一个Complete Action Using窗口,让我可以选择相机和放大器。库。

除了创建对话框之外,还有更简单的方法来实现此目的。

1 个答案:

答案 0 :(得分:2)

   AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setMessage(
                    context.getString(R.string.Select_an_Option_to_add_Photo))
                    .setCancelable(true)
                    .setPositiveButton(context.getString(R.string.Camera),
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    try {
                                        Intent action = new Intent(
                                                "android.media.action.IMAGE_CAPTURE");
                                        action.putExtra(
                                                MediaStore.EXTRA_OUTPUT,
                                                MediaStore.Images.Media.EXTERNAL_CONTENT_URI
                                                        .toString());
                                        startActivityForResult(action, 8);
                                    } catch (Exception e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                }

                            })
                    .setNegativeButton(context.getString(R.string.Gallery),
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog, int id) {
                                    dialog.cancel();

                                    try {
                                        Intent photoPickerIntent = new Intent(
                                                Intent.ACTION_GET_CONTENT);
                                        photoPickerIntent.setType("image/*");
                                        startActivityForResult(photoPickerIntent, 1);
                                    } catch (Exception e) {
                                        // TODO Auto-generated catch block
                                        e.printStackTrace();
                                    }
                                }
                            });
            alert = builder.create();

现在

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == RESULT_OK && requestCode == 8) {

            Bitmap photoBitMap = (Bitmap) data.getExtras().get("data");

            Bitmap usableBMP = Bitmap.createScaledBitmap(photoBitMap, 68, 80,
                    true);
//This is my ImageView Object           
cameraButton.setImageBitmap(usableBMP);
            cameraButton.setScaleType(ScaleType.CENTER_INSIDE);
        } else if (resultCode == RESULT_OK) {
            Uri chosenImageUri = data.getData();
            try {
//Here I scale my Bitmap as desired
                photoBitMap = Media.getBitmap(this.getContentResolver(),
                        chosenImageUri);
                Bitmap usableBMP = Bitmap.createScaledBitmap(photoBitMap, 68,
                        80, true);

//this is my ImageView Object
                cameraButton.setImageBitmap(usableBMP);
                cameraButton.setScaleType(ScaleType.CENTER_INSIDE);
            } catch (FileNotFoundException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }