質問編集履歴
2
書式の改善
test
CHANGED
@@ -1 +1 @@
|
|
1
|
-
|
1
|
+
アンドロイドアプリ開発で取得したUrlをBitmapに返還する方法で質問です。
|
test
CHANGED
@@ -1,284 +1,22 @@
|
|
1
|
-
|
1
|
+
いつもお世話になっております。
|
2
2
|
|
3
|
-
|
3
|
+
アンドロイドアプリ開発で取得したUrlをBitmapに変換する方法で質問です。
|
4
4
|
|
5
|
-
|
5
|
+
xmlでパースしたURLをBitmapに変換したいのですがBitmapへの変換がうまくいきません。
|
6
6
|
|
7
|
+
デバッグで見るとiamge_urlには画像のUrlが入っていますが次のstepで protected void layoutChildren() {
|
8
|
+
|
7
|
-
|
9
|
+
throw new RuntimeException("Stub!");
|
10
|
+
|
11
|
+
}に飛んでしまいます。
|
12
|
+
|
13
|
+
inputStreamへの渡し方が良くないのでしょうか?アドバイスを頂けますと幸いです。
|
8
14
|
|
9
15
|
|
10
16
|
|
11
|
-
RssParserTask.java
|
12
|
-
|
13
17
|
```
|
14
18
|
|
15
|
-
@Override
|
19
|
+
@Override
|
16
|
-
|
17
|
-
protected RssListAdapter doInBackground(String... params) {
|
18
|
-
|
19
|
-
RssListAdapter result = null;
|
20
|
-
|
21
|
-
try {
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
URL url = new URL(params[0]);
|
26
|
-
|
27
|
-
InputStream is = url.openConnection().getInputStream();
|
28
|
-
|
29
|
-
result = parseXml(is);
|
30
|
-
|
31
|
-
} catch (Exception e) {
|
32
|
-
|
33
|
-
e.printStackTrace();
|
34
|
-
|
35
|
-
}
|
36
|
-
|
37
|
-
return result;
|
38
|
-
|
39
|
-
}
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
@Override
|
44
|
-
|
45
|
-
protected void onPostExecute(RssListAdapter result) {
|
46
|
-
|
47
|
-
mProgressDialog.dismiss();
|
48
|
-
|
49
|
-
fragment.setListAdapter(result);
|
50
|
-
|
51
|
-
}
|
52
|
-
|
53
|
-
// XMLをパースする
|
54
|
-
|
55
|
-
public RssListAdapter parseXml(InputStream is) throws IOException,
|
56
|
-
|
57
|
-
XmlPullParserException {
|
58
|
-
|
59
|
-
XmlPullParser parser = Xml.newPullParser();
|
60
|
-
|
61
|
-
try {
|
62
|
-
|
63
|
-
parser.setInput(is, null);
|
64
|
-
|
65
|
-
int eventType = parser.getEventType();
|
66
|
-
|
67
|
-
Item currentItem = null;
|
68
|
-
|
69
|
-
while (eventType != XmlPullParser.END_DOCUMENT) {
|
70
|
-
|
71
|
-
String tag = null;
|
72
|
-
|
73
|
-
switch (eventType) {
|
74
|
-
|
75
|
-
case XmlPullParser.START_TAG:
|
76
|
-
|
77
|
-
tag = parser.getName();
|
78
|
-
|
79
|
-
if (tag.equals("item")) {
|
80
|
-
|
81
|
-
currentItem = new Item();
|
82
|
-
|
83
|
-
} else if (currentItem != null) {
|
84
|
-
|
85
|
-
if (tag.equals("title")) {
|
86
|
-
|
87
|
-
currentItem.setTitle(parser.nextText());
|
88
|
-
|
89
|
-
} else if (tag.equals("author")) {
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
currentItem.setSite((parser.nextText()));
|
94
|
-
|
95
|
-
} else if (tag.equals("pubDate")) {
|
96
|
-
|
97
|
-
currentItem.setDate((parser.nextText()));
|
98
|
-
|
99
|
-
} else if (tag.equals("link")) {
|
100
|
-
|
101
|
-
currentItem.setLink((parser.nextText()));
|
102
|
-
|
103
|
-
}else if (tag.equals("img")){
|
104
|
-
|
105
|
-
currentItem.setUrl(parser.getAttributeValue(null,"url"));
|
106
|
-
|
107
|
-
}
|
108
|
-
|
109
|
-
}
|
110
|
-
|
111
|
-
break;
|
112
|
-
|
113
|
-
case XmlPullParser.END_TAG:
|
114
|
-
|
115
|
-
tag = parser.getName();
|
116
|
-
|
117
|
-
if (tag.equals("item")) {
|
118
|
-
|
119
|
-
mAdapter.add(currentItem);
|
120
|
-
|
121
|
-
}
|
122
|
-
|
123
|
-
break;
|
124
|
-
|
125
|
-
}
|
126
|
-
|
127
|
-
eventType = parser.next();
|
128
|
-
|
129
|
-
}
|
130
|
-
|
131
|
-
} catch (Exception e) {
|
132
|
-
|
133
|
-
e.printStackTrace();
|
134
|
-
|
135
|
-
}
|
136
|
-
|
137
|
-
return mAdapter;
|
138
|
-
|
139
|
-
}
|
140
|
-
|
141
|
-
}
|
142
|
-
|
143
|
-
```
|
144
|
-
|
145
|
-
Item.java
|
146
|
-
|
147
|
-
```public class Item {
|
148
|
-
|
149
|
-
// 記事のタイトル
|
150
|
-
|
151
|
-
private CharSequence mTitle;
|
152
|
-
|
153
|
-
// 記事のサイト名
|
154
|
-
|
155
|
-
private CharSequence mSite;
|
156
|
-
|
157
|
-
// 記事の更新日
|
158
|
-
|
159
|
-
private CharSequence mDate;
|
160
|
-
|
161
|
-
// 記事のリンク
|
162
|
-
|
163
|
-
private CharSequence mLink;
|
164
|
-
|
165
|
-
// 記事の画像
|
166
|
-
|
167
|
-
private String mUrl;
|
168
|
-
|
169
|
-
|
170
|
-
|
171
|
-
|
172
|
-
|
173
|
-
|
174
|
-
|
175
|
-
public Item() {
|
176
|
-
|
177
|
-
mTitle = "";
|
178
|
-
|
179
|
-
mDate = "";
|
180
|
-
|
181
|
-
mSite ="";
|
182
|
-
|
183
|
-
mUrl = "";
|
184
|
-
|
185
|
-
}
|
186
|
-
|
187
|
-
|
188
|
-
|
189
|
-
public CharSequence getTitle() {
|
190
|
-
|
191
|
-
return mTitle;
|
192
|
-
|
193
|
-
}
|
194
|
-
|
195
|
-
|
196
|
-
|
197
|
-
public void setTitle(CharSequence title) {
|
198
|
-
|
199
|
-
mTitle = title;
|
200
|
-
|
201
|
-
}
|
202
|
-
|
203
|
-
|
204
|
-
|
205
|
-
public CharSequence getSite() {
|
206
|
-
|
207
|
-
return mSite;
|
208
|
-
|
209
|
-
}
|
210
|
-
|
211
|
-
|
212
|
-
|
213
|
-
public void setSite(CharSequence site) {
|
214
|
-
|
215
|
-
mSite = site;
|
216
|
-
|
217
|
-
}
|
218
|
-
|
219
|
-
|
220
|
-
|
221
|
-
public CharSequence getDate() {
|
222
|
-
|
223
|
-
return mDate;
|
224
|
-
|
225
|
-
}
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
public void setDate(CharSequence date) {
|
230
|
-
|
231
|
-
mDate = date;
|
232
|
-
|
233
|
-
}
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
public CharSequence getLink() {
|
238
|
-
|
239
|
-
return mLink;
|
240
|
-
|
241
|
-
}
|
242
|
-
|
243
|
-
|
244
|
-
|
245
|
-
public void setLink(CharSequence link) {
|
246
|
-
|
247
|
-
mLink = link;
|
248
|
-
|
249
|
-
}
|
250
|
-
|
251
|
-
public String getUrl(){
|
252
|
-
|
253
|
-
return mUrl;}
|
254
|
-
|
255
|
-
public void setUrl(String url) {
|
256
|
-
|
257
|
-
mUrl = url;
|
258
|
-
|
259
|
-
}
|
260
|
-
|
261
|
-
}
|
262
|
-
|
263
|
-
```
|
264
|
-
|
265
|
-
RssListAdapter.java
|
266
|
-
|
267
|
-
```
|
268
|
-
|
269
|
-
public RssListAdapter(Context context, List object){
|
270
|
-
|
271
|
-
super(context, 0, object);
|
272
|
-
|
273
|
-
mInflater = (LayoutInflater) context
|
274
|
-
|
275
|
-
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
276
|
-
|
277
|
-
}
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
@Override
|
282
20
|
|
283
21
|
public View getView(int position, View convertView, ViewGroup parent) {
|
284
22
|
|
@@ -312,25 +50,39 @@
|
|
312
50
|
|
313
51
|
|
314
52
|
|
315
|
-
String
|
53
|
+
String mImage = item.getImage().toString();
|
316
54
|
|
317
|
-
m
|
55
|
+
mImage = new String(mImage);
|
318
56
|
|
57
|
+
Image = (ImageView)view.findViewById(R.id.item_image);
|
58
|
+
|
59
|
+
BitmapFactory.Options options;
|
60
|
+
|
61
|
+
try{
|
62
|
+
|
63
|
+
URL iamge_url = new URL(mImage);
|
64
|
+
|
65
|
+
options = new BitmapFactory.Options();
|
66
|
+
|
67
|
+
InputStream inputStream = (InputStream)iamge_url.getContent();
|
68
|
+
|
69
|
+
mBitmap = BitmapFactory.decodeStream(inputStream,null,options);
|
70
|
+
|
71
|
+
inputStream.close();
|
72
|
+
|
319
|
-
m
|
73
|
+
Image.setImageBitmap(mBitmap);
|
320
74
|
|
321
75
|
|
322
76
|
|
323
|
-
|
324
|
-
|
325
|
-
|
77
|
+
}catch (MalformedURLException exception){
|
326
|
-
|
327
|
-
Bitmap bitmap = BitmapFactory.decodeStream(url);
|
328
78
|
|
329
79
|
|
330
80
|
|
331
|
-
|
81
|
+
}catch (IOException exception){
|
332
82
|
|
83
|
+
|
84
|
+
|
333
|
-
|
85
|
+
}
|
334
86
|
|
335
87
|
|
336
88
|
|
@@ -343,7 +95,3 @@
|
|
343
95
|
}
|
344
96
|
|
345
97
|
```
|
346
|
-
|
347
|
-
|
348
|
-
|
349
|
-
処理全体のコードはこんな感じになります。
|
1
書式・内容の編集
test
CHANGED
File without changes
|
test
CHANGED
@@ -1,31 +1,349 @@
|
|
1
1
|
アンドロイドアプリ開発で質問です。
|
2
2
|
|
3
|
+
http://itpro.nikkeibp.co.jp/article/COLUMN/20100105/342883/?ST=android-dev&P=1
|
4
|
+
|
3
|
-
RSSリーダーを作成しています。リストに画像を挿入したいと考えており、
|
5
|
+
こちらのサイトを参考にRSSリーダーを作成しています。リストに画像を挿入したいと考えており、
|
4
6
|
|
5
7
|
xmlでパースしたURLをBitmapに変換したいと思いますがどのように記載すればよいのでしょうか?
|
6
8
|
|
7
9
|
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
p
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
11
|
+
RssParserTask.java
|
12
|
+
|
13
|
+
```
|
14
|
+
|
15
|
+
@Override
|
16
|
+
|
17
|
+
protected RssListAdapter doInBackground(String... params) {
|
18
|
+
|
19
|
+
RssListAdapter result = null;
|
20
|
+
|
21
|
+
try {
|
22
|
+
|
23
|
+
|
24
|
+
|
25
|
+
URL url = new URL(params[0]);
|
26
|
+
|
27
|
+
InputStream is = url.openConnection().getInputStream();
|
28
|
+
|
29
|
+
result = parseXml(is);
|
30
|
+
|
31
|
+
} catch (Exception e) {
|
32
|
+
|
33
|
+
e.printStackTrace();
|
34
|
+
|
35
|
+
}
|
36
|
+
|
37
|
+
return result;
|
38
|
+
|
39
|
+
}
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
@Override
|
44
|
+
|
45
|
+
protected void onPostExecute(RssListAdapter result) {
|
46
|
+
|
47
|
+
mProgressDialog.dismiss();
|
48
|
+
|
49
|
+
fragment.setListAdapter(result);
|
50
|
+
|
51
|
+
}
|
52
|
+
|
53
|
+
// XMLをパースする
|
54
|
+
|
55
|
+
public RssListAdapter parseXml(InputStream is) throws IOException,
|
56
|
+
|
57
|
+
XmlPullParserException {
|
58
|
+
|
59
|
+
XmlPullParser parser = Xml.newPullParser();
|
60
|
+
|
61
|
+
try {
|
62
|
+
|
63
|
+
parser.setInput(is, null);
|
64
|
+
|
65
|
+
int eventType = parser.getEventType();
|
66
|
+
|
67
|
+
Item currentItem = null;
|
68
|
+
|
69
|
+
while (eventType != XmlPullParser.END_DOCUMENT) {
|
70
|
+
|
71
|
+
String tag = null;
|
72
|
+
|
73
|
+
switch (eventType) {
|
74
|
+
|
75
|
+
case XmlPullParser.START_TAG:
|
76
|
+
|
77
|
+
tag = parser.getName();
|
78
|
+
|
79
|
+
if (tag.equals("item")) {
|
80
|
+
|
81
|
+
currentItem = new Item();
|
82
|
+
|
83
|
+
} else if (currentItem != null) {
|
84
|
+
|
85
|
+
if (tag.equals("title")) {
|
86
|
+
|
87
|
+
currentItem.setTitle(parser.nextText());
|
88
|
+
|
89
|
+
} else if (tag.equals("author")) {
|
90
|
+
|
91
|
+
|
92
|
+
|
93
|
+
currentItem.setSite((parser.nextText()));
|
94
|
+
|
95
|
+
} else if (tag.equals("pubDate")) {
|
96
|
+
|
97
|
+
currentItem.setDate((parser.nextText()));
|
98
|
+
|
99
|
+
} else if (tag.equals("link")) {
|
100
|
+
|
101
|
+
currentItem.setLink((parser.nextText()));
|
102
|
+
|
103
|
+
}else if (tag.equals("img")){
|
104
|
+
|
105
|
+
currentItem.setUrl(parser.getAttributeValue(null,"url"));
|
106
|
+
|
107
|
+
}
|
108
|
+
|
109
|
+
}
|
110
|
+
|
111
|
+
break;
|
112
|
+
|
113
|
+
case XmlPullParser.END_TAG:
|
114
|
+
|
115
|
+
tag = parser.getName();
|
116
|
+
|
117
|
+
if (tag.equals("item")) {
|
118
|
+
|
119
|
+
mAdapter.add(currentItem);
|
120
|
+
|
121
|
+
}
|
122
|
+
|
123
|
+
break;
|
124
|
+
|
125
|
+
}
|
126
|
+
|
127
|
+
eventType = parser.next();
|
128
|
+
|
129
|
+
}
|
130
|
+
|
131
|
+
} catch (Exception e) {
|
132
|
+
|
133
|
+
e.printStackTrace();
|
134
|
+
|
135
|
+
}
|
136
|
+
|
137
|
+
return mAdapter;
|
138
|
+
|
139
|
+
}
|
140
|
+
|
141
|
+
}
|
142
|
+
|
143
|
+
```
|
144
|
+
|
145
|
+
Item.java
|
146
|
+
|
147
|
+
```public class Item {
|
148
|
+
|
149
|
+
// 記事のタイトル
|
150
|
+
|
151
|
+
private CharSequence mTitle;
|
152
|
+
|
153
|
+
// 記事のサイト名
|
154
|
+
|
155
|
+
private CharSequence mSite;
|
156
|
+
|
157
|
+
// 記事の更新日
|
158
|
+
|
159
|
+
private CharSequence mDate;
|
160
|
+
|
161
|
+
// 記事のリンク
|
162
|
+
|
163
|
+
private CharSequence mLink;
|
164
|
+
|
165
|
+
// 記事の画像
|
166
|
+
|
167
|
+
private String mUrl;
|
168
|
+
|
169
|
+
|
170
|
+
|
171
|
+
|
172
|
+
|
173
|
+
|
174
|
+
|
175
|
+
public Item() {
|
176
|
+
|
177
|
+
mTitle = "";
|
178
|
+
|
179
|
+
mDate = "";
|
180
|
+
|
181
|
+
mSite ="";
|
182
|
+
|
183
|
+
mUrl = "";
|
184
|
+
|
185
|
+
}
|
186
|
+
|
187
|
+
|
188
|
+
|
189
|
+
public CharSequence getTitle() {
|
190
|
+
|
191
|
+
return mTitle;
|
192
|
+
|
193
|
+
}
|
194
|
+
|
195
|
+
|
196
|
+
|
197
|
+
public void setTitle(CharSequence title) {
|
198
|
+
|
199
|
+
mTitle = title;
|
200
|
+
|
201
|
+
}
|
202
|
+
|
203
|
+
|
204
|
+
|
205
|
+
public CharSequence getSite() {
|
206
|
+
|
207
|
+
return mSite;
|
208
|
+
|
209
|
+
}
|
210
|
+
|
211
|
+
|
212
|
+
|
213
|
+
public void setSite(CharSequence site) {
|
214
|
+
|
215
|
+
mSite = site;
|
216
|
+
|
217
|
+
}
|
218
|
+
|
219
|
+
|
220
|
+
|
221
|
+
public CharSequence getDate() {
|
222
|
+
|
223
|
+
return mDate;
|
224
|
+
|
225
|
+
}
|
226
|
+
|
227
|
+
|
228
|
+
|
229
|
+
public void setDate(CharSequence date) {
|
230
|
+
|
231
|
+
mDate = date;
|
232
|
+
|
233
|
+
}
|
234
|
+
|
235
|
+
|
236
|
+
|
237
|
+
public CharSequence getLink() {
|
238
|
+
|
239
|
+
return mLink;
|
240
|
+
|
241
|
+
}
|
242
|
+
|
243
|
+
|
244
|
+
|
245
|
+
public void setLink(CharSequence link) {
|
246
|
+
|
247
|
+
mLink = link;
|
248
|
+
|
249
|
+
}
|
250
|
+
|
251
|
+
public String getUrl(){
|
252
|
+
|
253
|
+
return mUrl;}
|
254
|
+
|
255
|
+
public void setUrl(String url) {
|
256
|
+
|
257
|
+
mUrl = url;
|
258
|
+
|
259
|
+
}
|
260
|
+
|
261
|
+
}
|
262
|
+
|
263
|
+
```
|
264
|
+
|
265
|
+
RssListAdapter.java
|
266
|
+
|
267
|
+
```
|
268
|
+
|
269
|
+
public RssListAdapter(Context context, List object){
|
270
|
+
|
271
|
+
super(context, 0, object);
|
272
|
+
|
273
|
+
mInflater = (LayoutInflater) context
|
274
|
+
|
275
|
+
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
|
276
|
+
|
277
|
+
}
|
278
|
+
|
279
|
+
|
280
|
+
|
281
|
+
@Override
|
282
|
+
|
283
|
+
public View getView(int position, View convertView, ViewGroup parent) {
|
284
|
+
|
285
|
+
View view = convertView;
|
286
|
+
|
287
|
+
|
288
|
+
|
289
|
+
if (convertView == null) {
|
290
|
+
|
291
|
+
view = mInflater.inflate(R.layout.item_row, null);
|
292
|
+
|
293
|
+
}
|
294
|
+
|
295
|
+
|
296
|
+
|
297
|
+
|
298
|
+
|
299
|
+
// 現在参照しているリストの位置からItemを取得する
|
300
|
+
|
301
|
+
Item item = (Item) this.getItem(position);
|
302
|
+
|
303
|
+
if (item != null) {
|
304
|
+
|
305
|
+
// Itemから必要なデータを取り出し、それぞれTextViewにセットする
|
306
|
+
|
307
|
+
String title = item.getTitle().toString();
|
308
|
+
|
309
|
+
mTitle = (TextView) view.findViewById(R.id.item_title);
|
310
|
+
|
311
|
+
mTitle.setText(title);
|
312
|
+
|
313
|
+
|
314
|
+
|
315
|
+
String date = item.getDate().toString();
|
316
|
+
|
317
|
+
mDate = (TextView) view.findViewById(R.id.item_date);
|
318
|
+
|
319
|
+
mDate.setText(date);
|
320
|
+
|
321
|
+
|
322
|
+
|
323
|
+
|
324
|
+
|
325
|
+
String url = item.getUrl().toString();
|
326
|
+
|
327
|
+
Bitmap bitmap = BitmapFactory.decodeStream(url);
|
26
328
|
|
27
329
|
|
28
330
|
|
29
331
|
mImage =(ImageView)view.findViewById(R.id.item_image) ;
|
30
332
|
|
31
333
|
mImage.setImageBitmap(mBitmap);
|
334
|
+
|
335
|
+
|
336
|
+
|
337
|
+
}
|
338
|
+
|
339
|
+
return view;
|
340
|
+
|
341
|
+
}
|
342
|
+
|
343
|
+
}
|
344
|
+
|
345
|
+
```
|
346
|
+
|
347
|
+
|
348
|
+
|
349
|
+
処理全体のコードはこんな感じになります。
|