Android/기본
Android FileSearch Gallery(사진선택)
Bohemian life
2012. 4. 28. 19:52
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <ImageView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/photo" android:visibility="gone" android:layout_weight="1" /> <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/text" android:visibility="gone" android:layout_weight="0"/> <Button android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/button01" android:text="사진 선택" android:layout_weight="0" /> </LinearLayout>
package dr.android.file.search; import android.app.Activity; import android.os.Bundle; import java.io.File; import java.io.IOException; import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.net.Uri; import android.provider.MediaStore.Images; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; public class FileSearchbyGallery extends Activity implements OnClickListener { ImageView photo; Button button01; TextView text; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); photo = (ImageView) findViewById(R.id.photo); text = (TextView) findViewById(R.id.text); button01 = (Button) findViewById(R.id.button01); button01.setOnClickListener(this); } public void onClick(View v) { // 갤러리를 호출해서 휴대폰에 저장된 이미지를 읽어들임 Intent intent = new Intent(); intent.setAction(Intent.ACTION_GET_CONTENT); intent.setType("image/*");// image/jpg, image/png startActivityForResult(intent, 0); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (data != null) { try { Uri uri = data.getData(); // 이미지 정보 추출 및 표시 text.setText("========이미지 정보===\n"); text.append("URI : " + uri.toString() + "\n"); text.append("Last Path Segment : " + uri.getLastPathSegment() + "\n"); //컨텐트 프로바이더로 구성된 정보를 DB로 부터 읽어옴 Cursor c = getContentResolver().query( Images.Media.EXTERNAL_CONTENT_URI, null, Images.Media._ID + "=?", new String[] { uri.getLastPathSegment() }, null); if (c.moveToFirst()) { //getColumnIndexOrThrow : 컬럼의 인덱스 반환 String imageFile = c.getString(c .getColumnIndexOrThrow(Images.Media.DATA)); File f = new File(imageFile); text.append("원본 이미지 경로 : " + imageFile + "\n"); text.append("이미지 용량 : " + f.length() + "\n"); } text.setVisibility(View.VISIBLE); // ImageView 이미지 보여주기 //컨텐트 프로바이더로 구성된 정보를 URI를 통해 읽어옴 Bitmap image = Images.Media .getBitmap(getContentResolver(), uri); text.append("크기 : " + image.getWidth() + "*" + image.getHeight() + "\n"); photo.setImageBitmap(image); photo.setVisibility(View.VISIBLE); } catch (IOException e) { e.printStackTrace(); } } } }