Android Camera Intent - 内存不足错误和旋转错误

标签 android bitmap out-of-memory mediastore android-camera-intent

我正在创建一个 Android 应用程序,它利用相机拍照,然后根据用户输入将照片发送到服务器。我目前在相机 Intent 方面遇到了一些问题。我的主要问题是:

  1. 获取的照片与拍摄位置相比似乎发生了旋转。

  2. 当我尝试修复此 Rotation 时出现 OutOfMemoryError

因此,主要是我需要确保图片的方向不会改变,并且不会出现 OutOfMemoryError。

这里是使用相机 Intent 拍照的函数。

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

此函数用于旋转图像并在旋转时保存。

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 方法:

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

我已经尝试解决这个问题很长时间了,如果能得到一些帮助,我将不胜感激。谢谢!

最佳答案

修复内存不足错误的一种方法是使用 Bitmap.Factory Options,它有一个选项允许您将图像解码为缩放图像,这样它就不会占用太多内存,缺点是对此,生成的图像会稍微小一些。这是一个您可以尝试并可能对其进行微调以使其达到可接受的损失的设置。此外,您还需要在创建第二个位图后立即在第一个位图上调用 p.dispose()。请点击此链接查看示例:http://tutorials-android.blogspot.co.il/2011/11/outofmemory-exception-when-decoding.html

关于Android Camera Intent - 内存不足错误和旋转错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13203620/

相关文章:

android - 无法为图像或图片、 list 文件配置 BroadCast Receiver

Android - 线程与 AlarmManager

java - onSurfaceChanged 的​​目的是什么?

Android 位图缩放不正确

android - 如何使用位图作为通知图标

java - Solr Filter Cache(FastLRUCache)占用内存过多导致内存不足?

android - macOS Sierra 上 Android Studio 2.3.3 中的虚拟 SD 卡损坏错误

c# - 从网络摄像头抓取图像时,使用 XNA 渲染位图很慢,为什么?

android-studio - Flutter 和 Android Studio 中的内存不足错误

c# - 为什么我的 c# paint 方法内存不足?