Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.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
Java 为什么画廊中的肖像图像被保存为横向分析?_Java_Android_Parse Platform_Image Uploading_Image Rotation - Fatal编程技术网

Java 为什么画廊中的肖像图像被保存为横向分析?

Java 为什么画廊中的肖像图像被保存为横向分析?,java,android,parse-platform,image-uploading,image-rotation,Java,Android,Parse Platform,Image Uploading,Image Rotation,我从@Haresh Chhelana的文件夹中找到了下面的代码,并对其进行了一些修改,以便在将照片保存到云之前旋转从图库中选择的照片或使用Android手机拍摄的照片 当我用相机拍摄横向或纵向照片时,当我从Parse中检查时,它看起来总是正确的 当我从“多媒体资料”中选择一张风景照片时,照片将再次正确保存 但是,当我试图保存一张肖像照片时,该照片被保存为横向 请注意,在我的清单中有这些行,因此我的应用程序总是以纵向运行(我不知道这是否与问题有关): android:configChanges=

我从@Haresh Chhelana的文件夹中找到了下面的代码,并对其进行了一些修改,以便在将照片保存到云之前旋转从图库中选择的照片或使用Android手机拍摄的照片


当我用相机拍摄横向或纵向照片时,当我从Parse中检查时,它看起来总是正确的

当我从“多媒体资料”中选择一张风景照片时,照片将再次正确保存

但是,当我试图保存一张肖像照片时,该照片被保存为横向

请注意,在我的清单中有这些行,因此我的应用程序总是以纵向运行(我不知道这是否与问题有关):

android:configChanges=“方向” android:screenOrientation=“纵向”


这些是当用户想要从gallery上传图片或拍摄新图片时按下的按钮的侦听器:

picture.setOnClickListener(new View.OnClickListener() {
                @Override
                    public void onClick(View v) {
                        Intent intent = new Intent();
                        intent.setType("image/*");
                        intent.setAction(Intent.ACTION_GET_CONTENT);
                        startActivityForResult(Intent.createChooser(intent, ""), PICK_IMAGE);
                    flag_photo = true;
                    }
            });

            camera.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                    intent.putExtra(MediaStore.EXTRA_OUTPUT, setImageUri());
                    startActivityForResult(intent, CAPTURE_IMAGE);
                }
            });

在我的一个功能中保存照片的方式:

ParseFile file = null;

            file = new ParseFile("profile_picture.jpg", image);

            // Upload the image into Parse Cloud
            file.saveInBackground();

            user.put("photo", file);

OnActivityResult:

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == CAPTURE_IMAGE) {
                rotateImage(getImagePath());
            } else if (requestCode == PICK_IMAGE) {
//                imgFromCameraOrGallery.setImageBitmap(BitmapFactory.decodeFile(getAbsolutePath(data.getData())));

                // Convert it to byte
                ByteArrayOutputStream stream = new ByteArrayOutputStream();
                // Compress image to lower quality scale 1 - 100
                BitmapFactory.decodeFile(getAbsolutePath(data.getData())).compress(Bitmap.CompressFormat.JPEG, 100, stream);
                image = stream.toByteArray();

            }
            flag_photo = true;
        }
        else
            flag_photo = false;
    }

RotateImage函数:

  private void rotateImage(final String path) {
        runOnUiThread(new Runnable() {
            @Override
            public void run() {
                Bitmap b = decodeFileFromPath(path);
                try {
                    ExifInterface ei = new ExifInterface(path);
                    int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
                    Matrix matrix = new Matrix();
                    switch (orientation) {
                        case ExifInterface.ORIENTATION_ROTATE_90:
                            matrix.postRotate(90);
                            b = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), matrix, true);
                            break;
                        case ExifInterface.ORIENTATION_ROTATE_180:
                            matrix.postRotate(180);
                            b = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), matrix, true);
                            break;
                        case ExifInterface.ORIENTATION_ROTATE_270:
                            matrix.postRotate(270);
                            b = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), matrix, true);
                            break;
                        default:
                            b = Bitmap.createBitmap(b, 0, 0, b.getWidth(), b.getHeight(), matrix, true);
                            break;
                    }
                } catch (Throwable e) {
                    e.printStackTrace();
                }


                try {
                    String state = Environment.getExternalStorageState();
                    if (Environment.MEDIA_MOUNTED.equals(state)) {
                        file = new File(Environment.getExternalStorageDirectory() + "/DCIM/", "image" + new Date().getTime() + ".jpg");
                    }
                    else {
                        file = new File(getFilesDir() , "image" + new Date().getTime() + ".jpg");
                    }
                    out1 = new FileOutputStream(file);
                    b.compress(Bitmap.CompressFormat.JPEG, 100, out1);

                    // Convert it to byte
                    ByteArrayOutputStream stream = new ByteArrayOutputStream();

                    BitmapFactory.decodeFile(file.getAbsolutePath()).compress(Bitmap.CompressFormat.JPEG, 100, stream);

                    // Compress image to lower quality scale 1 - 100
                    image = stream.toByteArray();

                    //imgFromCameraOrGallery.setImageBitmap(BitmapFactory.decodeFile(file.getAbsolutePath()));
                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    try {
                        out1.close();
                    } catch (Throwable ignore) {

                    }
                }

            }
        });

    }

这是DecodeFileFromPath:

 private Bitmap decodeFileFromPath(String path){
        Uri uri = getImageUri(path);
        InputStream in = null;
        try {
            in = getContentResolver().openInputStream(uri);

            //Decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;

            BitmapFactory.decodeStream(in, null, o);
            in.close();


            int scale = 1;
            int inSampleSize = 1024;
            if (o.outHeight > inSampleSize || o.outWidth > inSampleSize) {
                scale = (int) Math.pow(2, (int) Math.round(Math.log(inSampleSize / (double) Math.max(o.outHeight, o.outWidth)) / Math.log(0.5)));
            }

            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize = scale;
            in = getContentResolver().openInputStream(uri);
            Bitmap b = BitmapFactory.decodeStream(in, null, o2);
            in.close();

            return b;

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

如果您想查看它们的代码,我也有这些函数。我没有发布它们,因为我的问题太长了,我不相信我可以改变它们中的某些东西来解决我的问题:

记下所有答案。。。旋转技术不止一种。您使用了错误的链接来检查是否需要旋转。我检查了您发送给我的链接以及答案导航到的链接。我尝试了我找到的所有可能的方法,最后唯一的例子来自@Haresh Chhelana。所以,我完全改变了上传照片的方式,但仍然没有旋转。我将张贴我的代码上面,并检查它,如果你想。