이미지 여러개 선택하여 이미지뷰에 보이기
java)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | package com.ghj.ex_003; import android.content.ClipData; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.provider.MediaStore; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends AppCompatActivity { //UI ImageView image1, image2, image3; //constant final int PICTURE_REQUEST_CODE = 100; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //UI image1 = (ImageView)findViewById(R.id.img1); image2 = (ImageView)findViewById(R.id.img2); image3 = (ImageView)findViewById(R.id.img3); Button btnImage = (Button)findViewById(R.id.btnImage); btnImage.setOnClickListener(new View.OnClickListener(){ @Override public void onClick(View v) { Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI); //사진을 여러개 선택할수 있도록 한다 intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); intent.setType("image/*"); startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICTURE_REQUEST_CODE); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode == PICTURE_REQUEST_CODE) { if (resultCode == RESULT_OK) { //기존 이미지 지우기 image1.setImageResource(0); image2.setImageResource(0); image3.setImageResource(0); //ClipData 또는 Uri를 가져온다 Uri uri = data.getData(); ClipData clipData = data.getClipData(); //이미지 URI 를 이용하여 이미지뷰에 순서대로 세팅한다. if(clipData!=null) { for(int i = 0; i < 3; i++) { if(i<clipData.getItemCount()){ Uri urione = clipData.getItemAt(i).getUri(); switch (i){ case 0: image1.setImageURI(urione); break; case 1: image2.setImageURI(urione); break; case 2: image3.setImageURI(urione); break; } } } } else if(uri != null) { image1.setImageURI(uri); } } } } } | cs |
xml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 | <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <Button android:id="@+id/btnImage" android:text="이미지 3개 가져오기" android:layout_width="wrap_content" android:layout_height="wrap_content"/> <ImageView android:id="@+id/img1" android:scaleType="center" android:layout_width="100dp" android:layout_height="100dp"/> <ImageView android:id="@+id/img2" android:scaleType="center" android:layout_width="100dp" android:layout_height="100dp"/> <ImageView android:id="@+id/img3" android:scaleType="center" android:layout_width="100dp" android:layout_height="100dp"/> </LinearLayout> | cs |
결과
'IT > - 프로그래밍' 카테고리의 다른 글
코코스 BMFont 사용하기 (0) | 2017.01.20 |
---|---|
코코스 외부 TTF Fonts 사용하기 (0) | 2017.01.19 |
코코스 Cocos Label 영역, 위치, 폰트, 폰트크기, 정렬 지정 (0) | 2017.01.17 |
안드로이드 Error:The number of method references in a .dex file cannot exceed 64. (0) | 2017.01.16 |
코코스 Cocos 텍스트 출력예제 (SystemFont 사용) (0) | 2017.01.13 |