位图 - 内存不足异常

2023-12-07

当我尝试从相机或图库获取图像时,出现错误。这是 logcat 的一部分:

06-27 05:51:47.297: E/dalvikvm-heap(438): Out of memory on a 35295376-byte allocation.
06-27 05:51:47.312: E/dalvikvm(438): Out of memory: Heap Size=108067KB, Allocated=71442KB, Limit=131072KB
06-27 05:51:47.312: E/dalvikvm(438): Extra info: Footprint=108067KB, Allowed Footprint=108067KB, Trimmed=56296KB
06-27 05:51:47.312: E/PowerManagerService(438): Excessive delay when setting lcd brightness: mLcdLight.setBrightness(176, 1) spend 288ms, mask=2
06-27 05:51:48.052: E/dalvikvm-heap(4332): Out of memory on a 24023056-byte allocation.
06-27 05:51:48.057: E/dalvikvm(4332): Out of memory: Heap Size=63139KB, Allocated=40922KB, Limit=65536KB
06-27 05:51:48.057: E/dalvikvm(4332): Extra info: Footprint=63139KB, Allowed Footprint=63139KB, Trimmed=0KB
06-27 05:51:48.057: E/EmbeddedLogger(438): App crashed! Process: <my_app_name>

这是我的代码,可让我拍摄图像:

Intent pickIntent = new Intent();
pickIntent.setType("image/*");
pickIntent.setAction(Intent.ACTION_GET_CONTENT);

Intent takePhotoIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

Intent chooserIntent = Intent.createChooser(pickIntent, "Select or take a new Picture");
chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { takePhotoIntent });
startActivityForResult(chooserIntent, selectPic);

and at onActivityResult() I do:

Bitmap bitmapSelectedImage = null;
Uri selectedImage =  data.getData();
String[] filePathColumn = { MediaStore.Images.Media.DATA };

Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();

String filePath = cursor.getString(cursor.getColumnIndex(filePathColumn[0]));
cursor.close();
bitmapSelectedImage = BitmapFactory.decodeFile(filePath); // Here is where do I get error.

我收到错误bitmapSelectedImage = BitmapFactory.decodeFile(filePath); line.

我查了很多网站/主题,但没有人能提供帮助。

有什么建议么?


您的内存分配堆大小非常有限。 尝试将高分辨率图像从文件加载到堆很容易导致内存不足错误。

假设相机应用程序确实采用非常高分辨率(几乎可以肯定是这种情况),您应该仅按照显示所需的大小将位图的缩放版本加载到内存中。

已建议您查看的文档 -http://developer.android.com/training/displaying-bitmaps/load-bitmap.html提供了完整的功能方法来做到这一点。

1)第一步是计算(不加载到内存)所需的比例。 那就是calculateInSampleSize method.

public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        // Calculate ratios of height and width to requested height and
        // width
        final int heightRatio = Math.round((float)height / (float)reqHeight);
        final int widthRatio = Math.round((float)width / (float)reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will
        // guarantee
        // a final image with both dimensions smaller than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}

2)第二步是使用步骤1的完整方法:

public static Bitmap getSampleBitmapFromFile(String bitmapFilePath, int reqWidth, int reqHeight) {
    // calculating image size
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(new FileInputStream(new File(bitmapFilePath)), null, options);

    int scale = calculateInSampleSize(options, reqWidth, reqHeight);

    BitmapFactory.Options o2 = new BitmapFactory.Options();
    o2.inSampleSize = scale;

    return BitmapFactory.decodeStream(new FileInputStream(new File(bitmapFilePath)), null, o2);

}

reqHeight and reqWith是显示图像的图像视图的高度和宽度(以像素为单位)。 假设您的图像视图是 100x100 像素,您需要做的就是:

bitmapSelectedImage = getSampleBitmapFromFile(filePath, 100, 100);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

位图 - 内存不足异常 的相关文章

随机推荐

  • Google Sheet QUERY + MATCHES 函数不排除字符串

    我想从排除某些字符的工作表中导入行 我首先使用 CONTAINS 函数执行此操作 但我没有找到使用多个参数执行此操作的方法 所以我使用 MATCHES 函数做到了 Query importrange URL Sheet a be SELEC
  • Visual Studio Code 缺少脏文件指示器和突出显示选项卡不起作用

    当 VS Code 中有脏文件时 我没有在文件选项卡中看到项目符号 也没有在选项卡上方看到突出显示栏 我在设置中打开了突出显示修改的选项卡 但没有任何效果 我还尝试更改主题 以防出现颜色问题 我没有任何会干扰这些设置的扩展 有人经历过这个吗
  • 以对象引用作为键的映射?

    我有一个存储有关特定实例的信息的对象 为此 我想使用Map 但由于键不是通过引用的 它们不是 对吧 而是作为由getHashCode方法 为了更好地理解 import collection mutable import java util
  • Function.call 方法作为回调[重复]

    这个问题在这里已经有答案了 如果我遗漏了什么 请原谅 但是当我尝试使用 call 方法作为回调时 它在 Chrome 和 Node js 中都会出现奇怪的错误 foo bar map String prototype trim call T
  • Rust 中的多种返回类型[重复]

    这个问题在这里已经有答案了 我有一个库函数f1 in rust返回一个字符串并想要更新它以选择性地返回一个向量 fn f1 gt String abc fn f2 gt String Vec
  • Hadoop kerberos 票证自动续订

    我在使用以下命令从 HDFS 下载大文件夹时遇到一些问题 hadoop fs get path to hdfs big folder 该文件夹很大 几乎 3TB kerberos 票证的生命周期为 10 小时 可续订生命周期为 7 天 下载
  • 如何将文件下载到 PHP 服务器?

    是否可以使用 PHP 脚本将远程服务器上的文件下载到我的 Web 服务器上 我有自己的网络服务器和域 我想在该域上放置一个 php 脚本 它将文件从远程服务器下载到我的服务器的文件系统上 这可能吗 Jim 当然 您需要文件系统上某处 您要保
  • 无法将图片从drawable发布到facebook

    我正在尝试将图像从可绘制文件夹传递到提要对话框 但我无法在 Facebook feed 对话中查看图像 其余参数可用 我正在使用 Facebook SDK 3 5 这是显示提要对话框的功能 private void publishFeedD
  • C++ - Qt - Visual Studio 2010 - 具有 GUI 和控制台的应用程序

    如果没有给程序提供任何参数 它将作为 GUI 应用程序启动 如果给定参数 它将通过命令行运行 我能够使用 Properties gt Linker gt SubSystem Console SUBSYSTEM CONSOLE 让 Visua
  • 添加一个新的可绘制对象,它正在更改解析的 xml 的图标

    我面临着一个只有在添加新的可绘制对象时才会发生的问题 我有一个已解析的xml to Fragment the icon设置为int 如果我添加新的可绘制对象 那么它会选择随机可绘制对象来显示已解析的图标xml 我有一个Adapter为了Re
  • 求 cos 的倒数

    at http www teacherschoice com au Maths Library Trigonometry solve trig SSS htm有 使用科学计算器求出 0 25 的反余弦 C cos 1 0 25 104 47
  • Laravel - 调用未定义的方法 Illuminate\Foundation\Application::share()

    我正在从 Laravel 5 3 升级到 Laravel 5 4 问题是当我跑步时composer update当谈到php artisan optimize部分 我收到错误 Symfony Component Debug Exceptio
  • 如何唤醒休眠线程并退出主线程?

    我正在创建 10 个线程 每个线程都会执行一些任务 有 7 项任务需要完成 由于任务数量小于线程数量 因此总会有 3 个线程处于休眠状态且不执行任何操作 我的主线程必须等待任务完成 只有当所有任务完成时 即线程退出时 才退出 我正在 for
  • 更改 tomcat 上 spring mvc 应用程序的应用程序根目录

    我正在 Spring MVC 3 0 上使用示例 RESTEasy 2 0 资源并部署到 Tomcat 6 我可以通过 http localhost 8080 examples resteasy 2 1 SNAPSHOT contacts
  • 使用 python 打印 Excel 工作簿

    假设我有一个excel文件excel file xlsx我想使用 Python 将其发送到我的打印机 所以我使用 import os os startfile path to file print 我的问题是 这仅打印 Excel 工作簿的
  • 如何搜索并替换内部包含等号“=”的字符串

    我必须从像 Pippo K 5 这样的 txt 中搜索字符串并将其替换为 Pippo K 1 我需要搜索整个字符串 我所做的是 set search Pippo K 5 set replace Pippo K 1 set textFile
  • 连接不同数据库中的多个表?数据库

    嘿 我正在寻找一种好方法 使用 php 连接到 mysql 中的至少 2 个数据库 并从表中收集每个数据库中的信息 这些表格将包含相关信息 例如 我在一个名为 sites 的表中的一个数据库 siteinfo 中有站点名称 我还在另一个数据
  • 如何更改数据透视表,使其以所需的方式显示数据?

    我已经能够将数据透视表拖到舞池上 但无法让它趴下来 似乎我们一直踩到对方的脚趾 我已经在工作表上获得了用于构建数据透视表的数据 此类描述了该数据 public class PriceVarianceData public String Un
  • Mac OS 上 Valgrind 下的 std::thread.join() SIGSEGV

    以下简单代码 C 11 将在 Mac OS 和 Linux 上运行 include
  • 位图 - 内存不足异常

    当我尝试从相机或图库获取图像时 出现错误 这是 logcat 的一部分 06 27 05 51 47 297 E dalvikvm heap 438 Out of memory on a 35295376 byte allocation 0