前提・実現したいこと
AndroidStudioとプログラミング初心者です。
Androidで二つのボタンを設定してそれぞれ押した時,同じ画面内に端末内の画像を画像を二つ表示させようとしています.
※イメージを追記しました.直撮りで申し訳ないです.現段階では上の画像しか表示されていない状態です.
発生している問題・エラーメッセージ
エラーメッセージは出てないのですが,一つの画像しか表示されなくて困っています.
string.xml
java
1 2<resources> 3 <string name="app_name">TestIntentGallery</string> 4 <string name="button">画像の取得</string> 5 <string name="button2">画像の取得2</string> 6 <string name="description">a picture</string> 7 <string name="description2">a picture2</string> 8</resources>
activity_main.xml
java
1<?xml version="1.0" encoding="utf-8"?> 2<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 android:orientation="vertical" 7 android:background="#fee" 8 android:gravity="center_horizontal" 9 tools:context=".MainActivity"> 10 11 <Button 12 android:id="@+id/button" 13 android:text="@string/button" 14 android:layout_margin="10dp" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" /> 17 18 <ImageView 19 android:id="@+id/image_view" 20 android:layout_width="match_parent" 21 android:layout_height="173dp" 22 android:layout_margin="10dp" 23 android:contentDescription="@string/description" 24 android:scaleType="center" /> 25 26 <Button 27 android:id="@+id/button2" 28 android:text="@string/button2" 29 android:layout_margin="10dp" 30 android:layout_width="wrap_content" 31 android:layout_height="wrap_content" /> 32 33 <ImageView 34 android:id="@+id/image_view2" 35 android:layout_width="match_parent" 36 android:layout_height="173dp" 37 android:layout_margin="10dp" 38 android:contentDescription="@string/description2" 39 android:scaleType="center" /> 40 41</LinearLayout>
MainActivity.java
java
1import android.app.Activity; 2import android.content.Intent; 3import android.graphics.Bitmap; 4import android.graphics.BitmapFactory; 5import android.media.Image; 6import android.net.Uri; 7import android.os.ParcelFileDescriptor; 8import android.support.v7.app.AppCompatActivity; 9import android.os.Bundle; 10import android.view.View; 11import android.widget.Button; 12import android.widget.ImageView; 13 14 15import java.io.FileDescriptor; 16import java.io.IOException; 17 18public class MainActivity extends AppCompatActivity { 19 20 // どこからIntentを飛ばしたかの判別するものでIntentを飛ばす時と、帰ってきた時に使う 21 private static final int RESULT_PICK_IMAGEFILE = 1001; 22 private static final int RESULT_PICK_IMAGEFILE2 = 1002; 23 // 画像を貼る場所 24 private ImageView imageView; 25 private ImageView imageView2; 26 27 28 @Override 29 protected void onCreate(Bundle savedInstanceState) { 30 //引数にスーパークラスの onCreate()メソッドを呼び出す 31 super.onCreate(savedInstanceState); 32 33 //画面に表示するビューを設定 34 //テキストボックスやボタンなど,画面表示をするコンテンツを管理しているオブジェクト 35 setContentView(R.layout.activity_main); 36 37 38 imageView = findViewById(R.id.image_view); 39 40 imageView2 = findViewById(R.id.image_view2); 41 42 Button button = findViewById(R.id.button); 43 44 Button button2 = findViewById(R.id.button2); 45 46 button.setOnClickListener(new View.OnClickListener() { 47 public void onClick(View v) { 48 //ピッカーを使用してファイルを選択するためのIntent 49 Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); 50 51 // openFileDescriptor()によるファイルストリームとして利用可能な「開くことができる」ファイルカテゴリーを選択 52 intent.addCategory(Intent.CATEGORY_OPENABLE); 53 54 // MIMEタイプを指定,取得するファイル形式をフィルターする 55 intent.setType("*/*"); 56 57 //結果画像の取得 58 startActivityForResult(intent, RESULT_PICK_IMAGEFILE); 59 } 60 }); 61 button2.setOnClickListener(new View.OnClickListener() { 62 public void onClick(View v) { 63 //ピッカーを使用してファイルを選択するためのIntent 64 Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT); 65 66 // openFileDescriptor()によるファイルストリームとして利用可能な「開くことができる」ファイルカテゴリーを選択 67 intent.addCategory(Intent.CATEGORY_OPENABLE); 68 69 // MIMEタイプを指定,取得するファイル形式をフィルターする 70 intent.setType("*/*"); 71 72 //開くアクティビティに対して何らかの情報を与える 73 startActivityForResult(intent, RESULT_PICK_IMAGEFILE2); 74 } 75 }); 76 } 77 78 79 @Override 80 //startActivityForResult 終了後 onActivityResult が呼ばれて, 81 // 「結果コード、呼び出し時のID、結果」が渡される 82 public void onActivityResult(int requestCode, int resultCode, Intent resultData) { 83 84 //要求コードREAD_REQUEST_CODEと共にACTION_OPEN_DOCUMENTインテントが送信された 85 //ここに表示されているリクエストコードが一致しない場合、他のコードは実行しない. 86 if (requestCode == RESULT_PICK_IMAGEFILE && resultCode == Activity.RESULT_OK) { 87 // ユーザーが選択したドキュメントはインテントで返されない 88 //代わりに、そのドキュメントへのURIは、このメソッドにパラメータとして返されるインテントに含まれる 89 //そのURI resultData.getData()を使用して取得する 90 91 if(resultData.getData() != null){ 92 93 ParcelFileDescriptor pfDescriptor = null; 94 try{ 95 Uri uri = resultData.getData(); 96 // Uriを表示 97 98 pfDescriptor = getContentResolver().openFileDescriptor(uri, "r"); 99 if(pfDescriptor != null){ 100 FileDescriptor fileDescriptor = pfDescriptor.getFileDescriptor(); 101 //Bitmapは Bitmap型として定義したインスタンス変数 102 Bitmap bmp = BitmapFactory.decodeFileDescriptor(fileDescriptor); 103 pfDescriptor.close(); 104 //imageViewはBitmapを表示させるためのインスタンス 105 imageView.setImageBitmap(bmp); 106 //imageView2.setImageBitmap(bmp); 107 108 } 109 } catch (IOException e) { 110 e.printStackTrace(); 111 } finally { 112 try{ 113 if(pfDescriptor != null){ 114 pfDescriptor.close(); 115 } 116 }catch (Exception e){ 117 e.printStackTrace(); 118 } 119 } 120 121 } 122 } 123 } 124 125 126 public void onActivityResult2(int requestCode2, int resultCode2, Intent resultData2) { 127 128 //要求コードREAD_REQUEST_CODEと共にACTION_OPEN_DOCUMENTインテントが送信された 129 //ここに表示されているリクエストコードが一致しない場合、他のコードは実行しない. 130 if (requestCode2 == RESULT_PICK_IMAGEFILE2 && resultCode2 == Activity.RESULT_OK) { 131 // ユーザーが選択したドキュメントはインテントで返されない 132 //代わりに、そのドキュメントへのURIは、このメソッドにパラメータとして返されるインテントに含まれる 133 //そのURI resultData.getData()を使用して取得する 134 135 if(resultData2.getData() != null){ 136 137 ParcelFileDescriptor pfDescriptor = null; 138 try{ 139 Uri uri = resultData2.getData(); 140 // Uriを表示 141 142 pfDescriptor = getContentResolver().openFileDescriptor(uri, "r"); 143 if(pfDescriptor != null){ 144 FileDescriptor fileDescriptor = pfDescriptor.getFileDescriptor(); 145 //Bitmapは Bitmap型として定義したインスタンス変数 146 Bitmap bmp = BitmapFactory.decodeFileDescriptor(fileDescriptor); 147 pfDescriptor.close(); 148 //imageViewはBitmapを表示させるためのインスタンス 149 150 imageView2.setImageBitmap(bmp); 151 } 152 } catch (IOException e) { 153 e.printStackTrace(); 154 } finally { 155 try{ 156 if(pfDescriptor != null){ 157 pfDescriptor.close(); 158 } 159 }catch (Exception e){ 160 e.printStackTrace(); 161 } 162 } 163 164 } 165 } 166 } 167}
試したこと
ボタンごとの処理に分けました