Android相机意图 - 内存不足错误和旋转错误 [英] Android Camera Intent - Out Of Memory Error and Rotation Error

查看:154
本文介绍了Android相机意图 - 内存不足错误和旋转错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建Android应用程序,它利用摄像头拍照,然后将它们发送到基于用户输入的服务器。我目前有一些问题相机意图。我的主要问题是:

I'm creating an android app that utilizes the camera to take pictures and then sends them to a server based on user input. I'm currently having some problems with the camera intent. My main problems are :


  1. 获取似乎比起它采取的立场时要旋转的照片。

  1. Getting a picture that seems to be rotated when compared to the position it was taken.

当我尝试解决这个旋转,我得到一个OutOfMemoryError

When I try to fix this Rotation I get an OutOfMemoryError

所以我主要是要确保该照片的方向不会改变,而且我没有得到一个OutOfMemoryError。

So mainly I need to make sure that the orientation of the picture doesn't change and that I don't get an OutOfMemoryError.

下面是使用相机意图拍摄照片的功能。

Here is the function that uses the camera Intent to take the picture.

private void dispatchTakePictureIntent(int actionCode) {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        File f = new File(Environment.getExternalStorageDirectory(),"/ServerApp/test.jpg");
        takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
        startActivityForResult(takePictureIntent, actionCode);
    }

和此功能用于旋转图像,并将其保存在该转动。

And this function is used to rotate the image and save it at that rotation.

private void getRotate() {

        String imagePath ="";
        int rotate = 0;
        File f = null;
        try {
             f = new File(Environment.getExternalStorageDirectory(),"/ServerApp/test.jpg");
             imagePath = f.getAbsolutePath();
            File imageFile = new File(imagePath);
            ExifInterface exif = new ExifInterface(
                    imageFile.getAbsolutePath());
            int orientation = exif.getAttributeInt(
                    ExifInterface.TAG_ORIENTATION,
                    ExifInterface.ORIENTATION_NORMAL);

            switch (orientation) {
            case ExifInterface.ORIENTATION_ROTATE_270:
                rotate = 270;
                break;
            case ExifInterface.ORIENTATION_ROTATE_180:
                rotate = 180;
                break;
            case ExifInterface.ORIENTATION_ROTATE_90:
                rotate = 90;
                break;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        Bitmap p = BitmapFactory.decodeFile(imagePath);

        Matrix m = new Matrix();
        m.setRotate(rotate);
        Bitmap _bitmapScaled = Bitmap.createBitmap(p, 0, 0, p.getWidth()/2,p.getHeight(), m, false);
        f.delete();



        ByteArrayOutputStream bytes = new ByteArrayOutputStream();
        _bitmapScaled.compress(Bitmap.CompressFormat.JPEG, 100, bytes);





        //you can create a new file name "test.jpg" in sdcard folder.
        File f1 = new File(Environment.getExternalStorageDirectory()
                                + File.separator + "test.jpg");
        try {
            f1.createNewFile();
            FileOutputStream fo = new FileOutputStream(f1);
            fo.write(bytes.toByteArray());

        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        //write the bytes in file

    }

最后,这是我的onActivityResult方法:

And finally, this is my onActivityResult method :

protected void onActivityResult(int requestCode, int resultCode, Intent data){
        getRotate();
        File f = new File(Environment.getExternalStorageDirectory(),"/ServerApp/test.jpg");
        mImageView.setImageBitmap(BitmapFactory.decodeFile(f.getAbsolutePath()));
    }

我一直在试图解决这个问题,很长一段时间,我会AP preciate它,如果我能得到一些帮助。谢谢!

I've been trying to solve this problem for a long time and I would appreciate it if I could get some help. Thanks!

推荐答案

解决您的内存不足的错误了是使用Bitmap.Factory选项A的方式,这有一个选项,让你脱code中的图像缩放图像,所以它不会使用这么多的内存,这样做的缺点是,生成的图像会小一些。它,你可以用和潜在的微调玩的设置得到它可以接受的损失。您还需要您创建了第二个位图后,立即打电话给p.dispose()在你的第一个位图。按照此链接的例子:的http://tutorials-android.blogspot.co.il/2011/11/outofmemory-exception-when-decoding.html

A way to fix your out of memory error would be to use Bitmap.Factory Options, this has an option that will allow you to decode the image as a scaled image so it won't use quite so much memory, the downside to this, is that the resulting image will be somewhat smaller. Its a setting that you could play with and potentially fine tune to get it to an acceptable loss. Also you will want to call p.dispose() on your first bitmap immediately after you have created your second bitmap. Follow this link for an example: http://tutorials-android.blogspot.co.il/2011/11/outofmemory-exception-when-decoding.html

这篇关于Android相机意图 - 内存不足错误和旋转错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆