{"id":909,"date":"2013-08-30T15:29:18","date_gmt":"2013-08-30T06:29:18","guid":{"rendered":"https:\/\/www.united-bears.co.jp\/blog\/?p=909"},"modified":"2013-08-30T15:29:18","modified_gmt":"2013-08-30T06:29:18","slug":"android-%e3%81%a7-bitmap-%e3%82%92%e5%ae%89%e5%85%a8%e3%81%ab%e6%93%8d%e4%bd%9c%e3%81%99%e3%82%8b1-%ef%bd%9e%e8%aa%ad%e3%81%bf%e8%be%bc%e3%81%bf%ef%bd%9e","status":"publish","type":"post","link":"https:\/\/www.united-bears.co.jp\/blog\/archives\/909","title":{"rendered":"Android \u3067 Bitmap \u3092\u5b89\u5168\u306b\u64cd\u4f5c\u3059\u308b(1) \uff5e\u8aad\u307f\u8fbc\u307f\uff5e"},"content":{"rendered":"<p>Android \u3067\u753b\u50cf\u3092\u6271\u3046\u5834\u5408 Bitmap \u30af\u30e9\u30b9\u3092\u5229\u7528\u3059\u308b\u304c\u3001OutOfMemoryError \u306b\u60a9\u307e\u3055\u308c\u308b\u3053\u3068\u304c\u591a\u3044\u3002Android \u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u304c\u5229\u7528\u3067\u304d\u308b\u30e1\u30e2\u30ea\u30fc\u306f16\uff5e64MB \u7a0b\u5ea6\u3067\u3001\u5927\u304d\u306a\u753b\u50cf\u3092\u8aad\u307f\u8fbc\u3081\u3070\u3059\u3050\u306b\u30e1\u30e2\u30ea\u30fc\u4e0d\u8db3\u306b\u306a\u3063\u3066\u3057\u307e\u3046\u3002\u305f\u3068\u3048\u3070\u30011024 x 768 \u306e\u5199\u771f\u3092 Config.ARGB_8888 \u3067\u8aad\u307f\u8fbc\u3080\u5834\u5408\u3001Config.ARGB_8888\u306f32bit \u306eARGB(Alpha, Red, Green, Blue)\u30c7\u30fc\u30bf\u306a\u306e\u3067\u300c1024 * 768 * 4 = 3145728byte = 3Mbyte\u300d\u306e\u30e1\u30e2\u30ea\u30fc\u304c\u5fc5\u8981\u306b\u306a\u308b\u3002\u5358\u7d14\u306b BitmapFactory.decodeStream(InputStream) \u3067\u305f\u304f\u3055\u3093\u306e\u753b\u50cf\u3092\u8aad\u307f\u8fbc\u3080\u3068\u3001\u3059\u3050\u306b java.lang.OutOfMemoryError \u304c\u51fa\u308b\u3002<\/p>\n<p>\u3053\u306e\u305f\u3081\u3001Android \u306e BitmapFactory \u306b\u306f\u5927\u304d\u306a\u753b\u50cf\u3092\u7e2e\u5c0f\u3057\u3066\u8aad\u307f\u8fbc\u3080 BitmapFactory.decodeStream(InputStream, Rect, BitmapFactory.Options)\u304c\u7528\u610f\u3055\u308c\u3066\u3044\u308b\u3002\u6700\u7d42\u7684\u306b\u7e2e\u5c0f\u3057\u3066\u8aad\u307f\u8fbc\u3080\u306e\u3067\u3042\u308c\u3070\u3001\u3053\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u4f7f\u3048\u3070\u3088\u3044\u3002\u307e\u305a\u3001\u300cinJustDecodeBounds = true\u300d\u3092\u6307\u5b9a\u3057\u3066\u753b\u50cf\u306e\u60c5\u5831\u3092\u8aad\u307f\u8fbc\u3080\u3002<\/p>\n<pre>\r\nInputStream in = context.getContentResolver().openInputStream(uri);\r\nBitmapFactory.Options options = new BitmapFactory.Options();\r\noptions.inJustDecodeBounds = true;\r\nBitmapFactory.decodeStream(in, null, options);\r\nin.close();\r\n<\/pre>\n<p>\u8aad\u307f\u8fbc\u3093\u3060\u753b\u50cf\u306e\u7e26\u5e45\u30fb\u6a2a\u5e45\u306f\u300coptions.outHeight\u300d\u300coptions.outWidth\u300d\u306b\u683c\u7d0d\u3055\u308c\u308b\u3002\u6b21\u306b\u7e2e\u5c0f\u3059\u308b\u5272\u5408\u3092\u6c7a\u3081\u3066 options.inSampleSize \u306b\u30bb\u30c3\u30c8\u3059\u308b\u3002desireHeight \u3068 desireWidth \u306f\u5e0c\u671b\u3059\u308b\u753b\u50cf\u306e\u7e26\u5e45\u3068\u6a2a\u5e45\u3060\u3002<\/p>\n<pre>\r\nfloat scaleX = options.outWidth \/ desireWidth;\r\nfloat scaleY = options.outHeight \/ desireHeight;\r\noptions.inSampleSize = (int) Math.floor(Float.valueOf(Math.max(scaleX, scaleY)).doubleValue());\r\n<\/pre>\n<p>inSampleSize \u304c1\u4ee5\u4e0a\u3067\u3042\u308c\u3070\u3001\u6307\u5b9a\u3055\u308c\u305f\u6570\u5024\u3067\u5272\u308a\u7b97\u3057\u3066\u30b5\u30a4\u30ba\u3067\u30c7\u30b3\u30fc\u30c9\u3059\u308b\u3002\u305f\u3068\u3048\u3070\u30014\u304c\u6307\u5b9a\u3055\u308c\u305f\u5834\u5408\u306f 1\/4 \u306e\u30b5\u30a4\u30ba\u306b\u306a\u308b\u3002\u6700\u5f8c\u306b\u3001options.inJustDecodeBounds \u306b true \u3092\u30bb\u30c3\u30c8\u3057\u3066\u5b9f\u969b\u306b\u753b\u50cf\u3092\u8aad\u307f\u8fbc\u3081\u3070\u3088\u3044\u3002<\/p>\n<pre>\r\noptions.inJustDecodeBounds = false;\r\nin = context.getContentResolver().openInputStream(uri);\r\nBitmap bitmap BitmapFactory.decodeStream(in, null, options);\r\nin.close();\r\n<\/pre>\n<p>inSampleSize \u306e\u5024\u306f\u30012\u306e\u3079\u304d\u4e57\u306b\u4e38\u3081\u3089\u308c\u308b\u306e\u3067\u6ce8\u610f\u304c\u5fc5\u8981\u3060\u3002<a href=\"http:\/\/developer.android.com\/reference\/android\/graphics\/BitmapFactory.Options.html#inSampleSize\" target=\"_blank\">BitmapFactory.Options \u306e inSampleSize \u306e\u8aac\u660e<\/a>\u306e\u6700\u5f8c\u306b\u6ce8\u610f\u66f8\u304d\u304c\u3042\u308b\u3002<\/p>\n<blockquote><p>\nNote: the decoder uses a final value based on powers of 2, any other value will be rounded down to the nearest power of 2.\n<\/p><\/blockquote>\n<p>\u3064\u307e\u308a\u3001inSampleSize \u306b4\u3092\u6307\u5b9a\u3057\u305f\u5834\u5408\u306f\u60f3\u5b9a\u901a\u308a1\/4\u306b\u306a\u308b\u304c\u30013\u3092\u6307\u5b9a\u3057\u305f\u5834\u5408\u306f1\/2\u306b\u30017\u3092\u6307\u5b9a\u3057\u305f\u5834\u5408\u306f1\/4\u306b\u306a\u308b\u3002\u5927\u304d\u306a\u753b\u50cf\u3092\u8aad\u307f\u8fbc\u3082\u3046\u3068\u3057\u305f\u3068\u304d\u307b\u3069\u3001\u5dee\u304c\u5927\u304d\u304f\u306a\u308b\u53ef\u80fd\u6027\u304c\u3042\u308b\u3002<\/p>\n<p>\u753b\u50cf\u3092\u5b89\u5168(OutOfMemoryError \u306a\u3057)\u306b\u8aad\u307f\u8fbc\u307f\u305f\u3044\u306e\u3067\u3042\u308c\u3070\u3001<\/p>\n<ul>\n<li>\u5229\u7528\u53ef\u80fd\u306a\u30e1\u30e2\u30ea\u30fc\u306e\u53d6\u5f97<\/li>\n<li>inSampleSize \u3067\u6307\u5b9a\u3057\u305f\u5024\u304c\u5b9f\u969b\u306b\u4f55\u306b\u306a\u308b\u304b\u3092\u628a\u63e1<\/li>\n<\/ul>\n<p>\u304c\u5fc5\u8981\u306b\u306a\u308b\u3002<\/p>\n<p>\u5229\u7528\u53ef\u80fd\u306a\u30e1\u30e2\u30ea\u30fc\u306e\u53d6\u5f97\u306f\u3001\u30cd\u30a4\u30c6\u30a3\u30d6\u306e\u30d2\u30fc\u30d7\u9818\u57df\u3082\u8003\u616e\u3059\u308b\u5fc5\u8981\u304c\u3042\u308b\u304b\u3082\u3057\u308c\u306a\u3044\u3002Runtime \u3068 Debug \u30af\u30e9\u30b9\u3092\u5229\u7528\u3057\u3066\u53d6\u5f97\u3057\u3066\u307f\u305f\u3002<\/p>\n<pre>\r\npublic static long getFreeMemory() {\r\n\tRuntime r = Runtime.getRuntime();\r\n\treturn r.maxMemory() - (r.totalMemory() - r.freeMemory())\r\n\t\t\t- Debug.getNativeHeapAllocatedSize();\r\n}\r\n<\/pre>\n<p>inSampleSize \u3067\u6307\u5b9a\u3057\u305f\u5024\u306f2\u306e\u3079\u304d\u4e57\u306b\u4e38\u3081\u3089\u308b\u306e\u3067\u3001\u8a08\u7b97\u3057\u3066\u6c42\u3081\u3089\u308c\u305d\u3046\u3060\u3002<\/p>\n<pre>\r\nint scale = options.inSampleSize;\r\nif (scale > 0 && (scale & (scale - 1)) == 0) {\t\/\/ scale \u304c2\u306e\u3079\u304d\u4e57\u306a\u3089\u3070\r\n\toptions.inSampleSize = scale;\r\n} else {\t\/\/ scale \u304c2\u306e\u3079\u304d\u4e57\u3067\u306a\u3044\u5834\u5408\u306f\u30012\u306e\u3079\u304d\u4e57\u306b\u4e38\u3081\u308b\r\n\toptions.inSampleSize = (int) Math.pow(2.0,\r\n\t\t\t(Math.floor(Math.log(scale - 1) \/ Math.log(2.0))));\r\n}\r\n<\/pre>\n<p>\u753b\u50cf\u3092\u8aad\u307f\u8fbc\u3080\u305f\u3081\u306b\u5fc5\u8981\u3068\u306a\u308b\u30e1\u30e2\u30ea\u30fc\u306e\u6982\u7b97\u306f\u3001<\/p>\n<pre>\r\n((options.outWidth \/ options.inSampleSize) * (options.outHeight \/ options.inSampleSize)) * 4;\r\n<\/pre>\n<p>\u306a\u306e\u3067\u3001getFreeMemory() \u30e1\u30bd\u30c3\u30c9\u3067\u5f97\u3089\u308c\u305f\u6700\u5927\u7a7a\u304d\u30e1\u30e2\u30ea\u30fc\u3068\u6bd4\u8f03\u3059\u308c\u3070\u3088\u3055\u305d\u3046\u3060\u3002\u7a7a\u304d\u30e1\u30e2\u30ea\u30fc\u306e\u65b9\u304c\u5c0f\u3055\u3044\u5834\u5408\u306f\u3001options.inSampleSize \u306b2\u3092\u304b\u3051\u3066\u3044\u3051\u3070\u3088\u3044\u3002\u3082\u3061\u308d\u3093\u3001\u753b\u50cf\u306f\u7c97\u304f\u306a\u308b\u3002\u7c97\u3044\u753b\u50cf\u306a\u3089\u3044\u3089\u306a\u3044\u3068\u3044\u3046\u5834\u5408\u306f\u3001\u7a7a\u304d\u30e1\u30e2\u30ea\u30fc\u304c\u8db3\u308a\u306a\u3044\u3068\u3044\u3046\u30a8\u30e9\u30fc\u3092\u8868\u793a\u3067\u304d\u308b\u3088\u3046\u306b\u3001\u3053\u306e\u30bf\u30a4\u30df\u30f3\u30b0\u3067\u8aad\u307f\u8fbc\u3093\u3060 Bitmap \u306e\u4ee3\u308f\u308a\u306b null \u3092\u623b\u3057\u3066\u3057\u307e\u3048\u3070\u3088\u3044\u3060\u308d\u3046\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Android \u3067\u753b\u50cf\u3092\u6271\u3046\u5834\u5408 Bitmap \u30af\u30e9\u30b9\u3092\u5229\u7528\u3059\u308b\u304c\u3001OutOfMemoryError \u306b\u60a9\u307e\u3055\u308c\u308b\u3053\u3068\u304c\u591a\u3044\u3002Android \u306e\u30a2\u30d7\u30ea\u30b1\u30fc\u30b7\u30e7\u30f3\u304c\u5229\u7528\u3067\u304d\u308b\u30e1\u30e2\u30ea\u30fc\u306f16\uff5e64MB \u7a0b\u5ea6\u3067\u3001\u5927\u304d\u306a\u753b\u50cf\u3092\u8aad [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,3],"tags":[],"class_list":["post-909","post","type-post","status-publish","format-standard","hentry","category-android","category-android-develop"],"_links":{"self":[{"href":"https:\/\/www.united-bears.co.jp\/blog\/wp-json\/wp\/v2\/posts\/909","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.united-bears.co.jp\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.united-bears.co.jp\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.united-bears.co.jp\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.united-bears.co.jp\/blog\/wp-json\/wp\/v2\/comments?post=909"}],"version-history":[{"count":18,"href":"https:\/\/www.united-bears.co.jp\/blog\/wp-json\/wp\/v2\/posts\/909\/revisions"}],"predecessor-version":[{"id":927,"href":"https:\/\/www.united-bears.co.jp\/blog\/wp-json\/wp\/v2\/posts\/909\/revisions\/927"}],"wp:attachment":[{"href":"https:\/\/www.united-bears.co.jp\/blog\/wp-json\/wp\/v2\/media?parent=909"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.united-bears.co.jp\/blog\/wp-json\/wp\/v2\/categories?post=909"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.united-bears.co.jp\/blog\/wp-json\/wp\/v2\/tags?post=909"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}