본문 바로가기

Daily Sample

[Day16] Android SD카드 MediaPlayer App만들기 소스

TITLE : Android SD card MediaPlayer App 

Date : 2010/07/27 

Writen by Dayyoung 

Description : 

This is Source that make the App play music & show Image by Intent service 

Download Link : 

Reference Site: Forget ; 

안드로이드에서 미디어 파일을 재생하거나 보여줄 때 가장 일반적인 방법은 

Content Provider를 통한 접근일 것 이다. 

Content Provider를 이용해 MediaProvider Manager를 호출하면, 

Cusor를 이용해 안드로이드폰에 미디어 파일에 접근할 수 있다. 

하지만,  때때로 어떤 이유에서 SD카드의 절대경로(AsolutePath)를 이용해 가져오고 싶을 때가 있다. 

1. Android Manifest 파일에서 SD카드의 읽기/쓰기 권한을 획득한다.   

 

2. getFilesList() 함수를 호출하여, SD카드 경로 /mp3폴더에 있는 mp3파일과 jpg파일 목록을 가져온다. 

getFilesList()함수는 JAVA.IO 클래스의 FILE 클래스를 이용하여 파일리스트를 가져온다. 

 FilenameFilter filter = new FilenameFilter() {
    public boolean accept(File file, String name) {
     return (name.endsWith(“.mp3″) || name.endsWith(“.jpg”));
    }
   };
fileList = dir.list(filter); 

 

3. File 클래스의 목록값은 String[] 배열로 반환되는데, Listview에 적용하기 위해서는 

ArrayList 형태로 캐스팅이 필요하다.  과정은 아래와 같다. 

  files = getFilesList(); 

private List<String> songs = new ArrayList<String>(); 

  if (files != null) { 

   for (int i = 0; i < files.length; i++) {
    // Add filename to service playlist
    songs.add(files[i]);
   } 

  } 

4. ListView의 음악 아이탬을 클릭했을 때, Intent는 내장플레이어를 호출하여 실행하게 된다. 

if (temp.endsWith(“.mp3″)) {
   Intent i = new Intent(android.content.Intent.ACTION_VIEW);
   i.setDataAndType(data, “audio/mp3″);
   startActivity(i);
  }  

 

5.  ListView의 사진 아이탬을 클릭했을 때, Intent는 겔러리뷰를 호출하여 실행하게 된다. 

else {
   Intent i = new Intent(android.content.Intent.ACTION_VIEW);
   i.setDataAndType(data, “image/*”);
   startActivity(i);
  }  

 

Creative Commons License
이 저작물은 크리에이티브 커먼즈 저작자표시 3.0 Unported 라이선스에 따라 이용할 수 있습니다