Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何拥有从画廊和相机拍摄的基本片段代码_Android_Android Fragments_Android Camera Intent - Fatal编程技术网

Android 如何拥有从画廊和相机拍摄的基本片段代码

Android 如何拥有从画廊和相机拍摄的基本片段代码,android,android-fragments,android-camera-intent,Android,Android Fragments,Android Camera Intent,我需要从相机和画廊拍摄照片,并完成某些功能,这需要从两个位置完成碎片 如何编写通用代码。是否可能,比如基类 private static final int PICK_IMAGE = 1; int REQUEST_CAMERA = 0; @Override public void onClick(View v) { switch (v.getId()) { case R.id.addProductImage:

我需要从相机和画廊拍摄照片,并完成某些功能,这需要从两个位置完成碎片

如何编写通用代码。是否可能,比如基类

 private static final int PICK_IMAGE = 1;
 int REQUEST_CAMERA = 0;

@Override
    public void onClick(View v) {
        switch (v.getId()) {
                case R.id.addProductImage:
                selectImage();
                break;

        }
    }

private void selectImage() {
        final CharSequence[] items = {"Take Photo", "Choose from Library", "Cancel"};
        AlertDialog.Builder builder = new AlertDialog.Builder(this.getActivity());
        builder.setTitle("Add Photo!");
        builder.setItems(items, new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialog, int item) {
                if (items[item].equals("Take Photo")) {
                    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    File file = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
                    startActivityForResult(intent, REQUEST_CAMERA);
                } else if (items[item].equals("Choose from Library")) {
                    selectImageFromGallery();
                } else if (items[item].equals("Cancel")) {
                    dialog.dismiss();
                }
            }
        });
        builder.show();
    }



     @Override
 public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == getActivity().RESULT_OK && requestCode == 1 && null != data) {
            decodeUri(data.getData());
        } else if (resultCode == getActivity().RESULT_OK && requestCode == REQUEST_CAMERA) {
            //  File file = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
            captureImage();
        }
    }
这种方法是从相机上捕获图像

 private void captureImage() {
            File file = new File(Environment.getExternalStorageDirectory() + File.separator + "image.jpg");
            bitmap = decodeSampledBitmapFromFile(file.getAbsolutePath(), 500, 250);

            // set the bitmap here to image view
              image.setImageBitmap(bitmap);



        }
 public static Bitmap decodeSampledBitmapFromFile(String path,
                                                     int reqWidth, int reqHeight) { // BEST QUALITY MATCH

        // First decode with inJustDecodeBounds=true to check dimensions
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);

        // Calculate inSampleSize
        // Raw height and width of image
        final int height = options.outHeight;
        final int width = options.outWidth;
        options.inPreferredConfig = Bitmap.Config.RGB_565;
        int inSampleSize = 1;

        if (height > reqHeight) {
            inSampleSize = Math.round((float) height / (float) reqHeight);
        }
        int expectedWidth = width / inSampleSize;
        if (expectedWidth > reqWidth) {
            //if(Math.round((float)width / (float)reqWidth) > inSampleSize) // If bigger SampSize..
            inSampleSize = Math.round((float) width / (float) reqWidth);
        }
        options.inSampleSize = inSampleSize;
        options.inJustDecodeBounds = false;
        return BitmapFactory.decodeFile(path, options);
    }
此方法用于从Galary中选择图像

public void selectImageFromGallery() {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE);
    }


 public void decodeUri(Uri uri) {
        ParcelFileDescriptor parcelFD = null;
        try {
            parcelFD = getActivity().getContentResolver().openFileDescriptor(uri, "r");
            FileDescriptor imageSource = parcelFD.getFileDescriptor();

            // Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeFileDescriptor(imageSource, null, o);

            // the new size we want to scale to
            final int REQUIRED_SIZE = 1024;

            // Find the correct scale value. It should be the power of 2.
            int width_tmp = o.outWidth, height_tmp = o.outHeight;
            int scale = 1;
            while (true) {
                if (width_tmp < REQUIRED_SIZE && height_tmp < REQUIRED_SIZE) {
                    break;
                }
                width_tmp /= 2;
                height_tmp /= 2;
                scale *= 2;
            }
            // decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;

            bitmap = BitmapFactory.decodeFileDescriptor(imageSource, null, o2);

                // set the bitmap here to image view

             image.setImageBitmap(bitmap);

        } catch (FileNotFoundException e) {
            Utility.showToat(mContext, " File Not Found Exception ");
        } finally {
            if (parcelFD != null)
                try {
                    parcelFD.close();
                } catch (IOException e) {
                    // ignored
                }
        }
    }

我想首先你需要决定片段的角色,它是否用于用户界面如果是的话,那么一旦你从相机或画廊获得了意图,就在一个地方处理它。目前我有两个片段,其中包含从相机拍照和在同一片段中更新的代码,而在另一种情况下,它需要显示在下一个片段中。嗨,德克斯,你能帮忙吗