質問編集履歴
3
ボタンごとの処理にわけました.
title
CHANGED
File without changes
|
body
CHANGED
@@ -70,6 +70,7 @@
|
|
70
70
|
import android.content.Intent;
|
71
71
|
import android.graphics.Bitmap;
|
72
72
|
import android.graphics.BitmapFactory;
|
73
|
+
import android.media.Image;
|
73
74
|
import android.net.Uri;
|
74
75
|
import android.os.ParcelFileDescriptor;
|
75
76
|
import android.support.v7.app.AppCompatActivity;
|
@@ -86,10 +87,12 @@
|
|
86
87
|
|
87
88
|
// どこからIntentを飛ばしたかの判別するものでIntentを飛ばす時と、帰ってきた時に使う
|
88
89
|
private static final int RESULT_PICK_IMAGEFILE = 1001;
|
90
|
+
private static final int RESULT_PICK_IMAGEFILE2 = 1002;
|
89
91
|
// 画像を貼る場所
|
90
92
|
private ImageView imageView;
|
91
|
-
|
93
|
+
private ImageView imageView2;
|
92
94
|
|
95
|
+
|
93
96
|
@Override
|
94
97
|
protected void onCreate(Bundle savedInstanceState) {
|
95
98
|
//引数にスーパークラスの onCreate()メソッドを呼び出す
|
@@ -101,11 +104,13 @@
|
|
101
104
|
|
102
105
|
|
103
106
|
imageView = findViewById(R.id.image_view);
|
104
|
-
imageView2 = findViewById(R.id.image_view2);
|
105
107
|
|
108
|
+
imageView2 = findViewById(R.id.image_view2);
|
109
|
+
|
106
110
|
Button button = findViewById(R.id.button);
|
107
|
-
Button button2 = findViewById(R.id.button2);
|
108
111
|
|
112
|
+
Button button2 = findViewById(R.id.button2);
|
113
|
+
|
109
114
|
button.setOnClickListener(new View.OnClickListener() {
|
110
115
|
public void onClick(View v) {
|
111
116
|
//ピッカーを使用してファイルを選択するためのIntent
|
@@ -117,7 +122,7 @@
|
|
117
122
|
// MIMEタイプを指定,取得するファイル形式をフィルターする
|
118
123
|
intent.setType("*/*");
|
119
124
|
|
120
|
-
//
|
125
|
+
//結果画像の取得
|
121
126
|
startActivityForResult(intent, RESULT_PICK_IMAGEFILE);
|
122
127
|
}
|
123
128
|
});
|
@@ -133,7 +138,7 @@
|
|
133
138
|
intent.setType("*/*");
|
134
139
|
|
135
140
|
//開くアクティビティに対して何らかの情報を与える
|
136
|
-
startActivityForResult(intent,
|
141
|
+
startActivityForResult(intent, RESULT_PICK_IMAGEFILE2);
|
137
142
|
}
|
138
143
|
});
|
139
144
|
}
|
@@ -161,10 +166,13 @@
|
|
161
166
|
pfDescriptor = getContentResolver().openFileDescriptor(uri, "r");
|
162
167
|
if(pfDescriptor != null){
|
163
168
|
FileDescriptor fileDescriptor = pfDescriptor.getFileDescriptor();
|
169
|
+
//Bitmapは Bitmap型として定義したインスタンス変数
|
164
170
|
Bitmap bmp = BitmapFactory.decodeFileDescriptor(fileDescriptor);
|
165
171
|
pfDescriptor.close();
|
172
|
+
//imageViewはBitmapを表示させるためのインスタンス
|
166
173
|
imageView.setImageBitmap(bmp);
|
167
|
-
|
174
|
+
//imageView2.setImageBitmap(bmp);
|
175
|
+
|
168
176
|
}
|
169
177
|
} catch (IOException e) {
|
170
178
|
e.printStackTrace();
|
@@ -182,8 +190,50 @@
|
|
182
190
|
}
|
183
191
|
}
|
184
192
|
|
193
|
+
|
194
|
+
public void onActivityResult2(int requestCode2, int resultCode2, Intent resultData2) {
|
195
|
+
|
196
|
+
//要求コードREAD_REQUEST_CODEと共にACTION_OPEN_DOCUMENTインテントが送信された
|
197
|
+
//ここに表示されているリクエストコードが一致しない場合、他のコードは実行しない.
|
198
|
+
if (requestCode2 == RESULT_PICK_IMAGEFILE2 && resultCode2 == Activity.RESULT_OK) {
|
199
|
+
// ユーザーが選択したドキュメントはインテントで返されない
|
200
|
+
//代わりに、そのドキュメントへのURIは、このメソッドにパラメータとして返されるインテントに含まれる
|
201
|
+
//そのURI resultData.getData()を使用して取得する
|
202
|
+
|
203
|
+
if(resultData2.getData() != null){
|
204
|
+
|
205
|
+
ParcelFileDescriptor pfDescriptor = null;
|
206
|
+
try{
|
207
|
+
Uri uri = resultData2.getData();
|
208
|
+
// Uriを表示
|
209
|
+
|
210
|
+
pfDescriptor = getContentResolver().openFileDescriptor(uri, "r");
|
211
|
+
if(pfDescriptor != null){
|
212
|
+
FileDescriptor fileDescriptor = pfDescriptor.getFileDescriptor();
|
213
|
+
//Bitmapは Bitmap型として定義したインスタンス変数
|
214
|
+
Bitmap bmp = BitmapFactory.decodeFileDescriptor(fileDescriptor);
|
215
|
+
pfDescriptor.close();
|
216
|
+
//imageViewはBitmapを表示させるためのインスタンス
|
217
|
+
|
218
|
+
imageView2.setImageBitmap(bmp);
|
219
|
+
}
|
220
|
+
} catch (IOException e) {
|
221
|
+
e.printStackTrace();
|
222
|
+
} finally {
|
223
|
+
try{
|
224
|
+
if(pfDescriptor != null){
|
225
|
+
pfDescriptor.close();
|
226
|
+
}
|
227
|
+
}catch (Exception e){
|
228
|
+
e.printStackTrace();
|
229
|
+
}
|
230
|
+
}
|
231
|
+
|
232
|
+
}
|
233
|
+
}
|
234
|
+
}
|
185
235
|
}
|
186
236
|
```
|
187
237
|
### 試したこと
|
188
238
|
|
189
|
-
|
239
|
+
ボタンごとの処理に分けました
|
2
画像選択ボタンのリスナクラスを分けてImageViewを2つ用意しました.Bitmapもう一つを用意しました.
title
CHANGED
File without changes
|
body
CHANGED
@@ -88,6 +88,7 @@
|
|
88
88
|
private static final int RESULT_PICK_IMAGEFILE = 1001;
|
89
89
|
// 画像を貼る場所
|
90
90
|
private ImageView imageView;
|
91
|
+
private ImageView imageView2;
|
91
92
|
|
92
93
|
@Override
|
93
94
|
protected void onCreate(Bundle savedInstanceState) {
|
@@ -100,43 +101,39 @@
|
|
100
101
|
|
101
102
|
|
102
103
|
imageView = findViewById(R.id.image_view);
|
104
|
+
imageView2 = findViewById(R.id.image_view2);
|
103
105
|
|
104
106
|
Button button = findViewById(R.id.button);
|
107
|
+
Button button2 = findViewById(R.id.button2);
|
108
|
+
|
105
109
|
button.setOnClickListener(new View.OnClickListener() {
|
106
110
|
public void onClick(View v) {
|
111
|
+
//ピッカーを使用してファイルを選択するためのIntent
|
107
|
-
|
112
|
+
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
|
108
|
-
case R.id.button:
|
109
113
|
|
110
|
-
|
114
|
+
// openFileDescriptor()によるファイルストリームとして利用可能な「開くことができる」ファイルカテゴリーを選択
|
111
|
-
|
115
|
+
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
112
116
|
|
113
|
-
|
117
|
+
// MIMEタイプを指定,取得するファイル形式をフィルターする
|
114
|
-
|
118
|
+
intent.setType("*/*");
|
115
119
|
|
120
|
+
//開くアクティビティに対して何らかの情報を与える
|
121
|
+
startActivityForResult(intent, RESULT_PICK_IMAGEFILE);
|
122
|
+
}
|
123
|
+
});
|
124
|
+
button2.setOnClickListener(new View.OnClickListener() {
|
125
|
+
public void onClick(View v) {
|
116
|
-
|
126
|
+
//ピッカーを使用してファイルを選択するためのIntent
|
117
|
-
|
127
|
+
Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
|
118
128
|
|
119
|
-
|
129
|
+
// openFileDescriptor()によるファイルストリームとして利用可能な「開くことができる」ファイルカテゴリーを選択
|
120
|
-
|
130
|
+
intent.addCategory(Intent.CATEGORY_OPENABLE);
|
121
131
|
|
132
|
+
// MIMEタイプを指定,取得するファイル形式をフィルターする
|
122
|
-
|
133
|
+
intent.setType("*/*");
|
123
134
|
|
124
|
-
case R.id.button2:
|
125
|
-
|
126
|
-
|
135
|
+
//開くアクティビティに対して何らかの情報を与える
|
127
|
-
Intent intent2 = new Intent(Intent.ACTION_OPEN_DOCUMENT);
|
128
|
-
|
129
|
-
// openFileDescriptor()によるファイルストリームとして利用可能な「開くことができる」ファイルカテゴリーを選択
|
130
|
-
intent2.addCategory(Intent.CATEGORY_OPENABLE);
|
131
|
-
|
132
|
-
// MIMEタイプを指定,取得するファイル形式をフィルターする
|
133
|
-
intent2.setType("*/*");
|
134
|
-
|
135
|
-
//結果の画像を取得するため,startActivityForResultで起動する
|
136
|
-
|
136
|
+
startActivityForResult(intent, RESULT_PICK_IMAGEFILE);
|
137
|
-
|
138
|
-
break;
|
139
|
-
}
|
140
137
|
}
|
141
138
|
});
|
142
139
|
}
|
@@ -167,6 +164,7 @@
|
|
167
164
|
Bitmap bmp = BitmapFactory.decodeFileDescriptor(fileDescriptor);
|
168
165
|
pfDescriptor.close();
|
169
166
|
imageView.setImageBitmap(bmp);
|
167
|
+
imageView2.setImageBitmap(bmp);
|
170
168
|
}
|
171
169
|
} catch (IOException e) {
|
172
170
|
e.printStackTrace();
|
1
イメージ画像の追記をしました.
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
### 前提・実現したいこと
|
2
2
|
AndroidStudioとプログラミング初心者です。
|
3
3
|
Androidで二つのボタンを設定してそれぞれ押した時,同じ画面内に端末内の画像を画像を二つ表示させようとしています.
|
4
|
+
※イメージを追記しました.直撮りで申し訳ないです.現段階では上の画像しか表示されていない状態です.
|
5
|
+

|
4
6
|
|
5
7
|
### 発生している問題・エラーメッセージ
|
6
8
|
|