Android更改上传照片方向

时间:2014-01-03 03:38:47

标签: android orientation photo

我找到了很多改变DISPLAY照片方向的解决方案,并取得了成功。但现在我需要使用file.path上传该照片。无论如何直接改变照片方向但不仅仅是以不同的方式显示?

CustomMultiPartEntity multipartContent = new CustomMultiPartEntity(
new CustomMultiPartEntity.ProgressListener() {
       @Override
       public void transferred(long num) {
       Constant.Process = (int) ((num / (float) totalSize11) * 100);}
});

multipartContent.addPart("key", new StringBody(Constant.Key));
multipartContent.addPart("userfile", new FileBody(new File(up.FilePath)));
httpPost.setEntity(multipartContent);
HttpResponse response1 = httpClient.execute(httpPost, httpContext);
response = EntityUtils.toString(response1.getEntity());

类似的东西,我需要通过FilePath将FileBody插入到'multipartContent'中,然后上传,无论如何都要上传正确的方向照片吗?

非常感谢!

编辑:这是我的onclick方法有摄像头,如何在保存之前添加代码来旋转图像?

public void onClick(View v) {
if (v == btn_camera) {
        Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

        Date date = new Date();
        DateFormat df = new SimpleDateFormat("yyyyMMddHHmmss");

        String newPicFile = "Miuzz"+ df.format(date) + ".jpg";
        String outPath = "/sdcard/" + newPicFile;
        File outFile = new File(outPath);

        mUri = Uri.fromFile(outFile);

        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, mUri);

        startActivityForResult(cameraIntent, 1);
    }
}

1 个答案:

答案 0 :(得分:1)

遗憾的是,除了加载图像,手动旋转并将其重新保存在正确的方向之外,无法修改照片文件的方向。

有关如何在图像旋转后保存图像的一些详细信息,请参阅此问题:Android Rotate Picture before saving

编辑:好的,所以你要做的就是你的onActivityResult()方法,加载图像并像你一样旋转它。旋转后,你应该有一个正确定位的Bitmap对象,然后你需要做的就是覆盖你现有的照片,或者像这样创建一个新文件:

try {
       FileOutputStream out = new FileOutputStream(filename);
       bitmap.compress(Bitmap.CompressFormat.PNG, 90, out);
       out.close();
} catch (Exception e) {
       e.printStackTrace();
}

另外值得注意的是,如果您决定创建新文件,请务必在上传完成后将其删除,否则您将不必要地填写用户的内部存储空间。