質問編集履歴
1
タイトルの変更、質問内容の修正
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
【質問内容修正】BitmapFactory.Optionsで画像を縮小して読み込もうとしたが上手く行かない
|
test
CHANGED
@@ -17,3 +17,109 @@
|
|
17
17
|
|
18
18
|
|
19
19
|
宜しくお願い致します。
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
### 追記
|
24
|
+
|
25
|
+
質問内容を修正します。
|
26
|
+
|
27
|
+
|
28
|
+
|
29
|
+
調べたところ、ダウンロードしてデータベースに保存し、ImageViewへ読み込むための画像のサイズが軒並み大きいため、縮小してから読み込もうと現在作業しています。
|
30
|
+
|
31
|
+
[https://qiita.com/exilias/items/38075e08ca45d223cf92](https://qiita.com/exilias/items/38075e08ca45d223cf92)
|
32
|
+
|
33
|
+
こちらのページで書かれていることを参考に、BitmapFactory.Optionsを使って縮小して読み込もうとしているのですが、10個ほどある画像のうち一つしかImageViewにセットできませんでした。
|
34
|
+
|
35
|
+
ページで書かれているのはJavaですが、Kotlinで以下のようなコードを書きました。
|
36
|
+
|
37
|
+
|
38
|
+
|
39
|
+
リサイクラービュアダプターのonBindViewHolder()の中に書いてあります。
|
40
|
+
|
41
|
+
holder.imageはカードビューの中に置いたImageViewです。
|
42
|
+
|
43
|
+
data[gazo_data]はデータベースから読み込んだ画像のデータです。
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
```Kotlin
|
48
|
+
|
49
|
+
|
50
|
+
|
51
|
+
val gazoData = Base64.decode(data["gazo_data"] as ByteArray, Base64.DEFAULT)
|
52
|
+
|
53
|
+
if (BitmapFactory.decodeByteArray(gazoData, 0, gazoData.size) == null) {
|
54
|
+
|
55
|
+
val inst = assets.open("noimage.jpg")
|
56
|
+
|
57
|
+
val noImage = BitmapFactory.decodeStream(inst)
|
58
|
+
|
59
|
+
holder.image?.setImageBitmap(noImage)
|
60
|
+
|
61
|
+
}else {
|
62
|
+
|
63
|
+
val options = BitmapFactory.Options().apply { inJustDecodeBounds = true }
|
64
|
+
|
65
|
+
val bitmap = BitmapFactory.decodeByteArray(gazoData, 0, gazoData.size, options)
|
66
|
+
|
67
|
+
val imageHeight = options.outHeight
|
68
|
+
|
69
|
+
val imageWidth = options.outWidth
|
70
|
+
|
71
|
+
val maxSize: Int = 500
|
72
|
+
|
73
|
+
val scaleWidth: Float = (imageWidth / maxSize).toFloat()
|
74
|
+
|
75
|
+
val scaleHeight: Float = (imageHeight / maxSize).toFloat()
|
76
|
+
|
77
|
+
|
78
|
+
|
79
|
+
if (scaleWidth > 2 && scaleHeight > 2) {
|
80
|
+
|
81
|
+
val imageOptions = BitmapFactory.Options()
|
82
|
+
|
83
|
+
val standValue = (if (scaleWidth > scaleHeight) {
|
84
|
+
|
85
|
+
scaleHeight
|
86
|
+
|
87
|
+
} else {
|
88
|
+
|
89
|
+
scaleWidth
|
90
|
+
|
91
|
+
}).toDouble()
|
92
|
+
|
93
|
+
val imageScale: Int = Math.floor(standValue).toInt()
|
94
|
+
|
95
|
+
var i = 2
|
96
|
+
|
97
|
+
while (i <= imageScale) {
|
98
|
+
|
99
|
+
imageOptions.inSampleSize = i
|
100
|
+
|
101
|
+
i *= 2
|
102
|
+
|
103
|
+
}
|
104
|
+
|
105
|
+
val bitmap2 = BitmapFactory.decodeByteArray(gazoData, 0, gazoData.size, imageOptions)
|
106
|
+
|
107
|
+
holder.image?.setImageBitmap(bitmap2)
|
108
|
+
|
109
|
+
} else {
|
110
|
+
|
111
|
+
holder.image?.setImageBitmap(bitmap)
|
112
|
+
|
113
|
+
}
|
114
|
+
|
115
|
+
}
|
116
|
+
|
117
|
+
|
118
|
+
|
119
|
+
```
|
120
|
+
|
121
|
+
|
122
|
+
|
123
|
+
どこかコードでおかしいところがあれば、指摘してくださると助かります。
|
124
|
+
|
125
|
+
宜しくお願い致します。
|