1

I am trying to take photo and let the photo saved in portrait orientation. I could not do so even if I changed the manifest nor the intent's orientation to set it portrait. I Google and tried various ways, but none works. Please assist.

 @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        Log.i("request", requestCode + "  result  " + resultCode + "  intent  "
                + data);
        super.onActivityResult(requestCode, resultCode, data);
        if(requestCode == RESULT_SAVE_ITEM){
            clearItemContents();
        }

        else if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK)
        {ExifInterface ei = new ExifInterface(cameraImagePath);
        int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_UNDEFINED);
        switch(orientation) {
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotateImage(bitmap, 90);
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotateImage(bitmap, 180);
                break;
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotateImage(bitmap, 270);
                break;
            case ExifInterface.ORIENTATION_NORMAL:
            default:
                break;
        }


            openGallery();
        }






        else{
            if (gallery == true) {
                if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK
                        && null != data) {
                    Uri selectedImage = data.getData();
                    String[] filePathColumn = { MediaStore.Images.Media.DATA };
                    Cursor cursor = getContentResolver().query(selectedImage,
                            filePathColumn, null, null, null);
                    cursor.moveToFirst();
                    int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
                    String picturePath = cursor.getString(columnIndex);
                    cursor.close();
                    imageArray = imageToByteArray(new File(picturePath));

                    setImagePicture(picturePath);
                } else {

                }
            } else if (gallery == false) {
                File file = new File(cameraImagePath);
                if (file.exists()) {
                    imageArray = imageToByteArray(file);
                    setImagePicture(cameraImagePath);
                } else {

                }

            } else {
                this.finish();
                startActivity(new Intent(this, DefectAdd.class));
            }}
    }
    public static Bitmap getRoundedCornerBitmap(Bitmap bitmap, int pixels)
    {
        Bitmap output = Bitmap.createBitmap(bitmap.getWidth(), bitmap
                .getHeight(), Config.ARGB_8888);
        Canvas canvas = new Canvas(output);

        final int color = 0xff424242;
        final Paint paint = new Paint();
        final Rect rect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        final RectF rectF = new RectF(rect);
        final float roundPx = pixels;

        paint.setAntiAlias(true);
        canvas.drawARGB(0, 0, 0, 0);
        paint.setColor(color);
        canvas.drawRoundRect(rectF, roundPx, roundPx, paint);

        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
        canvas.drawBitmap(bitmap, rect, rect, paint);

        return output;
    }


    private byte[] imageToByteArray(File file) {
        try {
            FileInputStream fis = new FileInputStream(file);


            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            byte[] buf = new byte[1024];

            for (int readNum; (readNum = fis.read(buf)) != -1;) {

                bos.write(buf, 0, readNum);
                System.out.println("read " + readNum + " bytes,");
            }
            byte[] bytes = bos.toByteArray();
            return bytes;
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        return null;
    }

    private void setImagePicture(String photoPath) {
        editTextItemPic.setVisibility(ImageView.GONE);
        itemImage.setVisibility(ImageView.VISIBLE);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        Bitmap bitmap = BitmapFactory.decodeFile(photoPath, options);
        itemImage.setRotation(90);
        itemImage.setImageBitmap(bitmap);
    }





    public void showDialog() {

        builder.setTitle("Choose Action!");
        builder.setItems(dialogItems, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int item) {

                if (item == 0) {
                    openGallery();
                } else if(item == 1) {
                    openCamera();


                }else{
                    imageArray = null;
                    editTextItemPic.setVisibility(ImageView.VISIBLE);
                    itemImage.setVisibility(ImageView.GONE);
                }
            }
        });
        AlertDialog alert = builder.create();
        alert.show();
    }

    public void openGallery() {
        MaterialAddActivity.gallery = true;
        Intent i = new Intent(Intent.ACTION_PICK,
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        startActivityForResult(i, MaterialAddActivity.RESULT_LOAD_IMAGE);
    }

    public void openCamera() {
        USE_CAMERA=1;

        Intent cameraIntent=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

        cameraIntent.putExtra(MediaStore.EXTRA_SCREEN_ORIENTATION, ActivityInfo.SCREEN_ORIENTATION_SENSOR_PORTRAIT);
        startActivityForResult(cameraIntent, CAMERA_REQUEST);

    }

    private String appFolderCheckandCreate() {

        String appFolderPath = "";
        File externalStorage =Environment.getExternalStorageDirectory();


        if (externalStorage.canWrite()) {
            appFolderPath = "/storage/emulated/0/DCIM/Camera/";
            File dir = new File(externalStorage+"/storage/emulated/0/DCIM/Camera/");


            if (!dir.exists()) {
                dir.mkdirs();
            }

        } else {

        }

        return appFolderPath;
    }

    @SuppressLint("SimpleDateFormat") private String getTimeStamp() {

        final long timestamp = new Date().getTime();

        final Calendar cal = Calendar.getInstance();
        cal.setTimeInMillis(timestamp);

        final String timeString = new SimpleDateFormat("HH_mm_ss_SSS")
                .format(cal.getTime());

        return timeString;
    }

    public void saveImage()
    {
        File filename;
        try
        {
            USE_CAMERA=0;
            String path = Environment.getExternalStorageDirectory().toString();

            new File(path + "/QSHelper").mkdirs();
            filename = new File(path + "/QSHelper/image"+LASTINSERTEDID+".png");

            FileOutputStream out = new FileOutputStream(filename);

            photo.compress(Bitmap.CompressFormat.PNG, 100, out);

            out.flush();
            out.close();

            MediaStore.Images.Media.insertImage(getContentResolver(),
                    filename.getAbsolutePath(), filename.getName(),
                    filename.getName());
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
Apple
  • 43
  • 1
  • 7
  • why you set itemImage.setRotation(90); on setImagePicture(String photoPath) method? – HendraWD Sep 19 '16 at 10:14
  • itemImage is my ImageView that I am showing the picture that is selected by user onto the application screen. But, if I export the image out to Excel file it retrieves the photo from gallery which will export the photo in a wrong orientation. That is why I want to save it in the correct orientation. – Apple Sep 19 '16 at 10:54
  • It is according to the camera hardware. http://stackoverflow.com/a/14066265/4804264 – Sujith Niraikulathan Sep 19 '16 at 11:08
  • Have you configure "exif orientation" when loading the photo to the image view? Because some phones model using exif metadata to save the orientation of the photo instead of rotating the photo directly – HendraWD Sep 20 '16 at 07:10
  • After I add the "exif orientation", it shows Unhandled Exception: java.io.IOException on `ExifInterface ei = new ExifInterface(cameraImagePath);` I have edited my post on top to show the new changes. – Apple Sep 20 '16 at 08:32

0 Answers0