먼저 SD카드에 있는 그림 이미지를 넣는 것에 이어서 실제로 Content Provider, Gallery View 등을 사용하여 SD카드에 있는 그림을 화면에 표시하는 코드를 작성해보았습니다. 짧은 코드지만 이 안에서 사용되는 Adapter, Content Provider에 관한 것은 안드로이드의 데이터 처리에서 가장 기본적인 것입니다.
Content Provider에 관해서는 http://code.google.com/android/devel/data/contentproviders.html 에서 자세히 설명되어 있습니다.
Code
public class Photo extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Gallery gallery = (Gallery) findViewById(R.id.gallery); Cursor c = getContentResolver().query(Images.Media.EXTERNAL_CONTENT_URI, null, null, null, null); //Image데이터 쿼리 ImageCursorAdapter adapter = new ImageCursorAdapter(this, c); gallery.setAdapter(adapter); } class ImageCursorAdapter extends CursorAdapter { public ImageCursorAdapter(Context context, Cursor c) { super(context, c); } @Override public void bindView(View view, Context context, Cursor cursor) { ImageView img = (ImageView)view; long id = cursor.getLong(cursor.getColumnIndexOrThrow(Images.Media._ID)); Uri uri = ContentUris.withAppendedId(Images.Media.EXTERNAL_CONTENT_URI, id); //개별 이미지에 대한 URI 생성 try { Bitmap bm = Images.Media.getBitmap(getContentResolver(), uri); //Bitmap 로드 img.setImageBitmap(bm); } catch(Exception e) {} } @Override public View newView(Context context, Cursor cursor, ViewGroup parent) { ImageView v = new ImageView(context); v.setLayoutParams(new Gallery.LayoutParams(80, 80)); v.setScaleType(ImageView.ScaleType.FIT_CENTER); return v; } } }
Layout
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" > <Gallery android:id="@+id/gallery" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentLeft="true" android:gravity="center_vertical" android:spacing="16dp" /> </LinearLayout>
덤으로 클릭시에 내장된 사진 뷰어를 뛰우려면
Intent i = new Intent(Intent.ACTION_VIEW, uri); startActivity(i);위와 같이 인텐트를 날리면 됩니다.
public
View newView(Context context, Cursor cursor, ViewGroup parent) 이 함수는 언제 어떻게 호출이 되나요?
아래 코드 처럼 하면 왜 안될까요?
무엇이 빠진 것이 있는지 잘못된 것이 있는지... 기본 도움말만 봐서는 통 알 수가 없네요
ImageView i = new ImageView(mContext); File file = new File("/sdcard/default.jpg"); Uri uri = Uri.fromFile(file); // uri -> file:///sdcard/default.jpg i.setImageURI(uri);
같은 uri를 가지고
위의 예처럼
Bitmap bm = Images.Media.getBitmap(getContentResolver(), uri); i.setImageBitmap(bm);
로 하면 로딩이 되네요 - -
imageView.setImageURI(u);
이렇게 하면 되네요.
실행시키면 Sorry! The application XXX has stopped unexpectedly. Please try again. 이라는 창이 뜨는데 왜그럴까요ㅜㅜ
log확인도 해보고 다른버전 에뮬레이터에서도 돌려봤는데 잘모르겠어요,ㅜㅜ
Images.Media.EXTERNAL_CONTENT_URI 이부분을
Images.Media.INTERNAL_CONTENT_URI 로 변경해서 폰(갤럭시S)에 어플설치후 확인해 보니까....
내장메모리 이미지를 못불러오고, 엉뚱하게 검정색바탕에 'T'로고가 있는 이미지만 불러오고 있습니다.
내장메모리의 경우 Images.Media.INTERNAL_CONTENT_URI로 하면 안되나요? 다른방법으로 해야 하는건가요?
버튼을 클릭하면 사진뷰어를 띄우도록 하려고 합니다. bindView메서드 안에
Button btnCall = (Button)findViewById(R.id.button1);
btnCall.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW, uri);
startActivity(i);}
});
이렇게 썼는데
위 소스중 Intent i = new Intent(Intent.ACTION_VIEW, uri); 부분
uri에 빨간줄이 쳐지면서 Cannot refer to a non-final variable uri inside an inner class defined in a different method라면서
change modifier of 'uri' to final 눌러서 고치라고 나오네요
고치고 실행해봤는데 에뮬에 사진이 한장만 들어가있을땐 잘 실행되지만
사진이 두장이상 들어가있으면 실행시 에러가 납니다
어떻게 고쳐야 될까요 ㅜ
SDCard에있는 비디오 경우에 path를 어떤 식으로 잡아야하나요?
문서에서 찾아보고있는데. 없는 관계로 질문 드리고 있습니다.
수고하세요.