티스토리 뷰
MainActivity.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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 | public class MainActivity extends AppCompatActivity { public ArrayList<RecyclerData> datas = null; private final static int REQUEST_CODE =100 ; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); datas = new ArrayList<>(); if( Build.VERSION.SDK_INT < Build.VERSION_CODES.M){ initData(); } else { checkPermissions(); } } // 권한이 허용되면 실행 public void initData(){ datas = getMusicInfo(); // 뮤직 데이터 셋팅 RecyclerView recyclerView = (RecyclerView) findViewById(R.id.recyclerView); RecyclerCardAdapter adapter = new RecyclerCardAdapter(datas,R.layout.card_item,this); recyclerView.setAdapter(adapter); RecyclerView.LayoutManager manager = new LinearLayoutManager(this); recyclerView.setLayoutManager(manager); } // 뮤직 데이터 셋팅 public ArrayList<RecyclerData> getMusicInfo(){ ArrayList<RecyclerData> datas = new ArrayList<>(); String [] projections = { MediaStore.Audio.Media._ID, // 노래아이디 MediaStore.Audio.Media.ALBUM_ID, // 앨범아이디 MediaStore.Audio.Media.TITLE, // 제목 MediaStore.Audio.Media.ARTIST, // 가수 }; //getContentResolver().query(주소, 검색해올 컴럼명들, 조건절, 조건절에 매핑되는 값, 정렬) Cursor cursor = getContentResolver().query( MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, projections, null, null, null ); /* - uri : content ://스키마 형태로 정해져 있는 곳의 데이터를 가져온다. - projection : 가져올 컬럼 이름들의 배열, null 을 입력하면 모든값을 가져온다. - selection : 조건절(where)에 해당하는 내용 - selectionArgs : 조건절이 preparedstatement 형태일 때 ? 에 매핑되는 값을 배열 - sort order : 정렬 조건 */ if( cursor != null ){ while(cursor.moveToNext()){ RecyclerData data = new RecyclerData(); int idx; // 데이터의 가수 이름을 입력 // 1. 가수 이름 컬럼의 순서 (index)를 가져온다. idx = cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST); // 2. 해당 index를 가진 컬럼의 실제값을 가져온다. data.artist = cursor.getString(idx); idx = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE); data.title = cursor.getString(idx); idx = cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID); data.albumId = cursor.getString(idx); idx = cursor.getColumnIndex(MediaStore.Audio.Media._ID); data.musicId = cursor.getString(idx); datas.add(data); } cursor.close(); // *반드시 종료* } return datas; } @TargetApi(Build.VERSION_CODES.M) private void checkPermissions(){ if (checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { // 쓰기 권한이 없으면 로직 처리 // 중간에 권한 내용에 대한 알림을 처리하는 함수 // shouldShowRequestPermissionRationale(); String permissionArray[] = {Manifest.permission.READ_EXTERNAL_STORAGE}; requestPermissions(permissionArray, REQUEST_CODE); }else{ // 쓰기권한이 있으면 파일생성 initData(); } } // 제네레이터 > 오버리아드 메소드 > onRequestPermissionsResult @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { super.onRequestPermissionsResult(requestCode, permissions, grantResults); switch (requestCode){ case REQUEST_CODE : if(grantResults[0] == PackageManager.PERMISSION_GRANTED){ initData(); } break; } } } | cs |
-----------------------------------------------------------------------------------------------------------------------------------------
manifest.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 | <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.baekcedar.android.medialibrary"> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL" /> <application android:allowBackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:supportsRtl="true" android:theme="@style/AppTheme"> <activity android:name=".MainActivity"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity> </application> </manifest> | cs |
-----------------------------------------------------------------------------------------------------------------------------------------
RecyclerCardAdapter.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 | public class RecyclerCardAdapter extends RecyclerView.Adapter<RecyclerCardAdapter.ViewHolder>{ ArrayList<RecyclerData> datas; int itemLayout; Context context; // 생성자 public RecyclerCardAdapter(ArrayList<RecyclerData> datas, int itemLayout, Context context){ this.datas = datas; this.itemLayout = itemLayout; this.context = context; } // view 를 만들어서 홀더에 저장하는 역할 @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View view = LayoutInflater.from(parent.getContext()).inflate(itemLayout, parent, false); return new ViewHolder(view); } // listView getView 를 대체하는 함수 @Override public void onBindViewHolder(ViewHolder holder, final int position) { RecyclerData data = datas.get(position); holder.cardView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { //클릭시 } }); holder.textName.setText(data.name); holder.textTitle.setText(data.title); holder.itemView.setTag(data); setAnimation(holder.cardView, position); } int lastPosision = -1; public void setAnimation(View view,int position){ if(position > lastPosision) { Animation ani = AnimationUtils.loadAnimation(context, android.R.anim.slide_in_left); view.startAnimation(ani); lastPosision = position; } } @Override public int getItemCount() { return datas.size(); } public static class ViewHolder extends RecyclerView.ViewHolder { TextView textName; TextView textTitle; CardView cardView; public ViewHolder(View itemView) { super(itemView); textName = (TextView) itemView.findViewById(R.id.textName); textTitle = (TextView) itemView.findViewById(R.id.textTitle); cardView = (CardView) itemView.findViewById(R.id.cardItem); } } } | cs |
https://github.com/baekcedar/MediaLibrary
'ANDROID > android' 카테고리의 다른 글
onFragmentInteraction (fragment 구분 확인) (0) | 2016.10.10 |
---|---|
Fragment 사용 (0) | 2016.10.05 |
Permission Runtime (0) | 2016.10.04 |
[android]Dp, Pixel 변환방법 (0) | 2016.10.04 |
annroid. VERSION CONTROL (0) | 2016.10.04 |
Comments