0

アクティビティに画像ボタンがあります。画像を 1 つずつ外部から読み込みたいと考えています。これは、最初の画像ボタンを除いて、最初はすべての画像ボタンが非表示になっていることを意味します。ボタンをクリックすると、画像をロードできるようになり、画像がボタンにロードされた後、次のボタンが表示されるはずです。最初のボタンでこれを達成しましたが、残りのボタンでは実行できませんでした。これは私のコードです。

public class MainActivity extends Activity {

private static int RESULT_LOAD_IMAGE = 1;

ImageButton a, b, c, d, e, f;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    a = (ImageButton) findViewById(R.id.imageButton1);
    b = (ImageButton) findViewById(R.id.imageButton2);
    c = (ImageButton) findViewById(R.id.imageButton3);
    d = (ImageButton) findViewById(R.id.imageButton4);
    e = (ImageButton) findViewById(R.id.imageButton5);
    f = (ImageButton) findViewById(R.id.imageButton6);

    a.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(
                    Intent.ACTION_PICK,
                    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

            startActivityForResult(intent, RESULT_LOAD_IMAGE);
        }
    });



@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
        Uri selectedImage = data.getData();
        String[] filePathColumn = { MediaStore.Images.Media.DATA };

        Cursor cursor = getContentResolver().query(selectedImage,
                filePathColumn, null, null, null);
        cursor.moveToFirst();

        int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
        String picturePath = cursor.getString(columnIndex);
        cursor.close();


            ImageButton a = (ImageButton)findViewById(R.id.imageButton1);
            a.setImageBitmap(BitmapFactory.decodeFile(picturePath));

            b.setVisibility(b.VISIBLE);


    }

}
4

2 に答える 2