質問編集履歴

3

ボタンごとの処理にわけました.

2019/02/01 06:31

投稿

oyasai3
oyasai3

スコア10

test CHANGED
File without changes
test CHANGED
@@ -142,6 +142,8 @@
142
142
 
143
143
  import android.graphics.BitmapFactory;
144
144
 
145
+ import android.media.Image;
146
+
145
147
  import android.net.Uri;
146
148
 
147
149
  import android.os.ParcelFileDescriptor;
@@ -174,11 +176,15 @@
174
176
 
175
177
  private static final int RESULT_PICK_IMAGEFILE = 1001;
176
178
 
179
+ private static final int RESULT_PICK_IMAGEFILE2 = 1002;
180
+
177
181
  // 画像を貼る場所
178
182
 
179
183
  private ImageView imageView;
180
184
 
181
-   private ImageView imageView2;
185
+ private ImageView imageView2;
186
+
187
+
182
188
 
183
189
 
184
190
 
@@ -204,13 +210,17 @@
204
210
 
205
211
  imageView = findViewById(R.id.image_view);
206
212
 
213
+
214
+
207
-      imageView2 = findViewById(R.id.image_view2);
215
+ imageView2 = findViewById(R.id.image_view2);
208
216
 
209
217
 
210
218
 
211
219
  Button button = findViewById(R.id.button);
212
220
 
221
+
222
+
213
-      Button button2 = findViewById(R.id.button2);
223
+ Button button2 = findViewById(R.id.button2);
214
224
 
215
225
 
216
226
 
@@ -236,43 +246,137 @@
236
246
 
237
247
 
238
248
 
249
+ //結果画像の取得
250
+
251
+ startActivityForResult(intent, RESULT_PICK_IMAGEFILE);
252
+
253
+ }
254
+
255
+ });
256
+
257
+ button2.setOnClickListener(new View.OnClickListener() {
258
+
259
+ public void onClick(View v) {
260
+
261
+ //ピッカーを使用してファイルを選択するためのIntent
262
+
263
+ Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
264
+
265
+
266
+
267
+ // openFileDescriptor()によるファイルストリームとして利用可能な「開くことができる」ファイルカテゴリーを選択
268
+
269
+ intent.addCategory(Intent.CATEGORY_OPENABLE);
270
+
271
+
272
+
273
+ // MIMEタイプを指定,取得するファイル形式をフィルターする
274
+
275
+ intent.setType("*/*");
276
+
277
+
278
+
239
279
  //開くアクティビティに対して何らかの情報を与える
240
280
 
241
- startActivityForResult(intent, RESULT_PICK_IMAGEFILE);
281
+ startActivityForResult(intent, RESULT_PICK_IMAGEFILE2);
242
282
 
243
283
  }
244
284
 
245
285
  });
246
286
 
287
+ }
288
+
289
+
290
+
291
+
292
+
293
+ @Override
294
+
295
+ //startActivityForResult 終了後 onActivityResult が呼ばれて,
296
+
297
+ // 「結果コード、呼び出し時のID、結果」が渡される
298
+
299
+ public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
300
+
301
+
302
+
303
+ //要求コードREAD_REQUEST_CODEと共にACTION_OPEN_DOCUMENTインテントが送信された
304
+
305
+ //ここに表示されているリクエストコードが一致しない場合、他のコードは実行しない.
306
+
307
+ if (requestCode == RESULT_PICK_IMAGEFILE && resultCode == Activity.RESULT_OK) {
308
+
309
+ // ユーザーが選択したドキュメントはインテントで返されない
310
+
311
+ //代わりに、そのドキュメントへのURIは、このメソッドにパラメータとして返されるインテントに含まれる
312
+
313
+ //そのURI resultData.getData()を使用して取得する
314
+
315
+
316
+
317
+ if(resultData.getData() != null){
318
+
319
+
320
+
321
+ ParcelFileDescriptor pfDescriptor = null;
322
+
323
+ try{
324
+
325
+ Uri uri = resultData.getData();
326
+
327
+ // Uriを表示
328
+
329
+
330
+
331
+ pfDescriptor = getContentResolver().openFileDescriptor(uri, "r");
332
+
333
+ if(pfDescriptor != null){
334
+
247
- button2.setOnClickListener(new View.OnClickListener() {
335
+ FileDescriptor fileDescriptor = pfDescriptor.getFileDescriptor();
336
+
248
-
337
+ //Bitmapは Bitmap型として定義したインスタンス変数
338
+
339
+ Bitmap bmp = BitmapFactory.decodeFileDescriptor(fileDescriptor);
340
+
249
- public void onClick(View v) {
341
+ pfDescriptor.close();
250
-
342
+
251
- //ピッカー使用してファイルを選択するためのIntent
343
+ //imageViewはBitmap表示させるためのインスタンス
252
-
253
- Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
344
+
254
-
255
-
256
-
257
- // openFileDescriptor()によるファイルストリームとして利用可能な「開くことができる」ファイルカテゴリーを選択
258
-
259
- intent.addCategory(Intent.CATEGORY_OPENABLE);
345
+ imageView.setImageBitmap(bmp);
260
-
261
-
262
-
346
+
263
- // MIMEタイプを指定,取得するファイル形式をフィルターする
347
+ //imageView2.setImageBitmap(bmp);
348
+
349
+
350
+
264
-
351
+ }
352
+
353
+ } catch (IOException e) {
354
+
355
+ e.printStackTrace();
356
+
357
+ } finally {
358
+
359
+ try{
360
+
265
- intent.setType("*/*");
361
+ if(pfDescriptor != null){
266
-
267
-
268
-
269
- //開くアクティビティに対して何らかの情報を与える
362
+
270
-
271
- startActivityForResult(intent, RESULT_PICK_IMAGEFILE);
363
+ pfDescriptor.close();
364
+
365
+ }
366
+
367
+ }catch (Exception e){
368
+
369
+ e.printStackTrace();
370
+
371
+ }
372
+
373
+ }
374
+
375
+
272
376
 
273
377
  }
274
378
 
275
- });
379
+ }
276
380
 
277
381
  }
278
382
 
@@ -280,13 +384,7 @@
280
384
 
281
385
 
282
386
 
283
- @Override
284
-
285
- //startActivityForResult 終了後 onActivityResult が呼ばれて,
286
-
287
- // 「結果コード、呼び出し時のID、結果」が渡される
288
-
289
- public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
387
+ public void onActivityResult2(int requestCode2, int resultCode2, Intent resultData2) {
290
388
 
291
389
 
292
390
 
@@ -294,7 +392,7 @@
294
392
 
295
393
  //ここに表示されているリクエストコードが一致しない場合、他のコードは実行しない.
296
394
 
297
- if (requestCode == RESULT_PICK_IMAGEFILE && resultCode == Activity.RESULT_OK) {
395
+ if (requestCode2 == RESULT_PICK_IMAGEFILE2 && resultCode2 == Activity.RESULT_OK) {
298
396
 
299
397
  // ユーザーが選択したドキュメントはインテントで返されない
300
398
 
@@ -304,7 +402,7 @@
304
402
 
305
403
 
306
404
 
307
- if(resultData.getData() != null){
405
+ if(resultData2.getData() != null){
308
406
 
309
407
 
310
408
 
@@ -312,7 +410,7 @@
312
410
 
313
411
  try{
314
412
 
315
- Uri uri = resultData.getData();
413
+ Uri uri = resultData2.getData();
316
414
 
317
415
  // Uriを表示
318
416
 
@@ -324,13 +422,17 @@
324
422
 
325
423
  FileDescriptor fileDescriptor = pfDescriptor.getFileDescriptor();
326
424
 
425
+ //Bitmapは Bitmap型として定義したインスタンス変数
426
+
327
427
  Bitmap bmp = BitmapFactory.decodeFileDescriptor(fileDescriptor);
328
428
 
329
429
  pfDescriptor.close();
330
430
 
331
- imageView.setImageBitmap(bmp);
431
+ //imageViewBitmapを表示させるためのインスタンス
332
-
432
+
433
+
434
+
333
-                imageView2.setImageBitmap(bmp);
435
+ imageView2.setImageBitmap(bmp);
334
436
 
335
437
  }
336
438
 
@@ -364,8 +466,6 @@
364
466
 
365
467
  }
366
468
 
367
-
368
-
369
469
  }
370
470
 
371
471
  ```
@@ -374,4 +474,4 @@
374
474
 
375
475
 
376
476
 
377
- switchでViewのIDごと処理分けました
477
+ ボタンごと処理分けました

2

画像選択ボタンのリスナクラスを分けてImageViewを2つ用意しました.Bitmapもう一つを用意しました.

2019/02/01 06:31

投稿

oyasai3
oyasai3

スコア10

test CHANGED
File without changes
test CHANGED
@@ -178,6 +178,8 @@
178
178
 
179
179
  private ImageView imageView;
180
180
 
181
+   private ImageView imageView2;
182
+
181
183
 
182
184
 
183
185
  @Override
@@ -202,174 +204,168 @@
202
204
 
203
205
  imageView = findViewById(R.id.image_view);
204
206
 
207
+      imageView2 = findViewById(R.id.image_view2);
208
+
205
209
 
206
210
 
207
211
  Button button = findViewById(R.id.button);
208
212
 
213
+      Button button2 = findViewById(R.id.button2);
214
+
215
+
216
+
209
217
  button.setOnClickListener(new View.OnClickListener() {
210
218
 
211
219
  public void onClick(View v) {
212
220
 
213
- switch (v.getId()){
214
-
215
- case R.id.button:
216
-
217
-
218
-
219
- //ピッカを使用してファイルを選択するためのIntent
220
-
221
- Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
222
-
223
-
224
-
225
- // openFileDescriptor()によるファイルストリームとして利用可能な「開くことができる」ァイカテゴリを選択
226
-
227
- intent.addCategory(Intent.CATEGORY_OPENABLE);
228
-
229
-
230
-
231
- // MIMEタイプ指定,取得すファイル形式をフィルターする
232
-
233
- intent.setType("*/*");
234
-
235
-
236
-
237
- //結果の画像を取得するため,startActivityForResultで起動する
238
-
239
- startActivityForResult(intent, RESULT_PICK_IMAGEFILE);
240
-
241
-
242
-
243
- break;
244
-
245
-
246
-
247
- case R.id.button2:
248
-
249
-
250
-
251
- //ピッカーを使用してファイルを選択するためのIntent
252
-
253
- Intent intent2 = new Intent(Intent.ACTION_OPEN_DOCUMENT);
254
-
255
-
256
-
257
- // openFileDescriptor()によるファイルストリームとして利用可能な「開くことができる」ファイルカテゴリーを選択
258
-
259
- intent2.addCategory(Intent.CATEGORY_OPENABLE);
260
-
261
-
262
-
263
- // MIMEタイプを指定,取得するファイル形式をフィルターする
264
-
265
- intent2.setType("*/*");
266
-
267
-
268
-
269
- //結果の画像を取得するため,startActivityForResultで起動する
270
-
271
- startActivityForResult(intent2, RESULT_PICK_IMAGEFILE);
272
-
273
-
274
-
275
- break;
221
+ //ピッカーを使用してファイルを選択するためのIntent
222
+
223
+ Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
224
+
225
+
226
+
227
+ // openFileDescriptor()によるファイルストリムとして利用可能な「開くことができる」ファイルカテゴリーを選択
228
+
229
+ intent.addCategory(Intent.CATEGORY_OPENABLE);
230
+
231
+
232
+
233
+ // MIMEタイプを指定,取得するファイル形式をする
234
+
235
+ intent.setType("*/*");
236
+
237
+
238
+
239
+ //開くアクティビティに対して何らかの情報与え
240
+
241
+ startActivityForResult(intent, RESULT_PICK_IMAGEFILE);
242
+
243
+ }
244
+
245
+ });
246
+
247
+ button2.setOnClickListener(new View.OnClickListener() {
248
+
249
+ public void onClick(View v) {
250
+
251
+ //ピッカーを使用してファイルを選択するためのIntent
252
+
253
+ Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
254
+
255
+
256
+
257
+ // openFileDescriptor()によるファイルストリームとして利用可能な「開くことができる」ファイルカテゴリーを選択
258
+
259
+ intent.addCategory(Intent.CATEGORY_OPENABLE);
260
+
261
+
262
+
263
+ // MIMEタイプを指定,取得するファイル形式をフィルターする
264
+
265
+ intent.setType("*/*");
266
+
267
+
268
+
269
+ //開くアクティビティに対して何らかの情報を与える
270
+
271
+ startActivityForResult(intent, RESULT_PICK_IMAGEFILE);
272
+
273
+ }
274
+
275
+ });
276
+
277
+ }
278
+
279
+
280
+
281
+
282
+
283
+ @Override
284
+
285
+ //startActivityForResult 終了後 onActivityResult が呼ばれて,
286
+
287
+ // 「結果コード、呼び出し時のID、結果」が渡される
288
+
289
+ public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
290
+
291
+
292
+
293
+ //要求コードREAD_REQUEST_CODEと共にACTION_OPEN_DOCUMENTインテントが送信された
294
+
295
+ //ここに表示されているリクエストコードが一致しない場合、他のコードは実行しない.
296
+
297
+ if (requestCode == RESULT_PICK_IMAGEFILE && resultCode == Activity.RESULT_OK) {
298
+
299
+ // ユーザーが選択したドキュメントはインテントで返されない
300
+
301
+ //代わりに、そのドキュメントへのURIは、このメソッドにパラメータとして返されるインテントに含まれる
302
+
303
+ //そのURI resultData.getData()を使用して取得する
304
+
305
+
306
+
307
+ if(resultData.getData() != null){
308
+
309
+
310
+
311
+ ParcelFileDescriptor pfDescriptor = null;
312
+
313
+ try{
314
+
315
+ Uri uri = resultData.getData();
316
+
317
+ // Uriを表示
318
+
319
+
320
+
321
+ pfDescriptor = getContentResolver().openFileDescriptor(uri, "r");
322
+
323
+ if(pfDescriptor != null){
324
+
325
+ FileDescriptor fileDescriptor = pfDescriptor.getFileDescriptor();
326
+
327
+ Bitmap bmp = BitmapFactory.decodeFileDescriptor(fileDescriptor);
328
+
329
+ pfDescriptor.close();
330
+
331
+ imageView.setImageBitmap(bmp);
332
+
333
+                imageView2.setImageBitmap(bmp);
334
+
335
+ }
336
+
337
+ } catch (IOException e) {
338
+
339
+ e.printStackTrace();
340
+
341
+ } finally {
342
+
343
+ try{
344
+
345
+ if(pfDescriptor != null){
346
+
347
+ pfDescriptor.close();
348
+
349
+ }
350
+
351
+ }catch (Exception e){
352
+
353
+ e.printStackTrace();
354
+
355
+ }
276
356
 
277
357
  }
278
358
 
359
+
360
+
279
361
  }
280
362
 
281
- });
363
+ }
282
364
 
283
365
  }
284
366
 
285
367
 
286
368
 
287
-
288
-
289
- @Override
290
-
291
- //startActivityForResult 終了後 onActivityResult が呼ばれて,
292
-
293
- // 「結果コード、呼び出し時のID、結果」が渡される
294
-
295
- public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
296
-
297
-
298
-
299
- //要求コードREAD_REQUEST_CODEと共にACTION_OPEN_DOCUMENTインテントが送信された
300
-
301
- //ここに表示されているリクエストコードが一致しない場合、他のコードは実行しない.
302
-
303
- if (requestCode == RESULT_PICK_IMAGEFILE && resultCode == Activity.RESULT_OK) {
304
-
305
- // ユーザーが選択したドキュメントはインテントで返されない
306
-
307
- //代わりに、そのドキュメントへのURIは、このメソッドにパラメータとして返されるインテントに含まれる
308
-
309
- //そのURI resultData.getData()を使用して取得する
310
-
311
-
312
-
313
- if(resultData.getData() != null){
314
-
315
-
316
-
317
- ParcelFileDescriptor pfDescriptor = null;
318
-
319
- try{
320
-
321
- Uri uri = resultData.getData();
322
-
323
- // Uriを表示
324
-
325
-
326
-
327
- pfDescriptor = getContentResolver().openFileDescriptor(uri, "r");
328
-
329
- if(pfDescriptor != null){
330
-
331
- FileDescriptor fileDescriptor = pfDescriptor.getFileDescriptor();
332
-
333
- Bitmap bmp = BitmapFactory.decodeFileDescriptor(fileDescriptor);
334
-
335
- pfDescriptor.close();
336
-
337
- imageView.setImageBitmap(bmp);
338
-
339
- }
340
-
341
- } catch (IOException e) {
342
-
343
- e.printStackTrace();
344
-
345
- } finally {
346
-
347
- try{
348
-
349
- if(pfDescriptor != null){
350
-
351
- pfDescriptor.close();
352
-
353
- }
354
-
355
- }catch (Exception e){
356
-
357
- e.printStackTrace();
358
-
359
- }
360
-
361
- }
362
-
363
-
364
-
365
- }
366
-
367
- }
368
-
369
- }
370
-
371
-
372
-
373
369
  }
374
370
 
375
371
  ```

1

イメージ画像の追記をしました.

2019/01/30 09:07

投稿

oyasai3
oyasai3

スコア10

test CHANGED
File without changes
test CHANGED
@@ -4,6 +4,10 @@
4
4
 
5
5
  Androidで二つのボタンを設定してそれぞれ押した時,同じ画面内に端末内の画像を画像を二つ表示させようとしています.
6
6
 
7
+ ※イメージを追記しました.直撮りで申し訳ないです.現段階では上の画像しか表示されていない状態です.
8
+
9
+ ![イメージ](d74d72a9a76e0a76e44462f7ff78b305.jpeg)
10
+
7
11
 
8
12
 
9
13
  ### 発生している問題・エラーメッセージ