开发者

Android Complete Action Using

开发者 https://www.devze.com 2023-04-11 17:39 出处:网络
Hi I have this button and when I click on it I\'d like to launch a Complete Action Using window that would allow me to choose between Camera & Gallery.

Hi I have this button and when I click on it I'd like to launch a Complete Action Using window that would allow me to choose between Camera & Gallery.

Is there an easier way to implement this other than creating a dialo开发者_JS百科g.


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

Now

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

    }
0

精彩评论

暂无评论...
验证码 换一张
取 消