-1

I need to take pictures from camera and gallery and do certain some functionality,This needs to be done from two places(fragments),

How can i write common code.Is it possible,Like a base class?

Rakesh
  • 14,997
  • 13
  • 42
  • 62
  • I guess first you need to decide the role of fragment, whether it is used for UI purpose if yes then once you get intent either from Camera or Gallery processed it at one place. – dex Dec 07 '15 at 09:10
  • Currently i have 2 fragments which contains code to take picture from camera and Update in same fragment,and while in other it needs to be displayed in next fragment . – Rakesh Dec 07 '15 at 09:55
  • Hi dex,can you help on this – Rakesh Dec 07 '15 at 13:41

1 Answers1

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

This Method is to capture image from Camera

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

This method is to select image from 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
                }
        }
    }
Pavan Bilagi
  • 1,618
  • 1
  • 18
  • 23