/** * Share Music * * @param id * @param context */ public static void shareMusic(long id, Context context) { if (permissionManager.isExternalReadStorageGranted(context)) { if (id == 0) { return; } Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; Uri trackUri = Uri.parse(uri.toString() + "/" + id); Intent intent = new Intent(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_STREAM, trackUri); intent.setType("audio/*"); context.startActivity(Intent.createChooser(intent, context.getString(R.string.share))); } else { Log.d("Helper", "Permission failed"); } }
/** * return song metadata from path * @param context * @param path * @return */ public static List<Song> getSongMetaData(Context context, String path) { if (path == null){ return null; } List<Song> songList = new ArrayList<>(); DefaultSongLoader defaultSongLoader = new DefaultSongLoader(context); defaultSongLoader.setProvider(true); defaultSongLoader.setUri(Uri.parse(String.valueOf(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI))); defaultSongLoader.setQueryTable(null); for (String ext : fileExtensions) { if (path.toLowerCase().endsWith(ext)) { defaultSongLoader.setSelection(MediaStore.Audio.Media.DATA + " like ? "); defaultSongLoader.setQueryTable2(new String[]{"%" + path + "%"}); defaultSongLoader.setSortOrder(null); songList.add(defaultSongLoader.getSongData()); } } return songList; }
/** * return real path of image * * @param uri * @param context * @return */ public static String getRealPathFromURI(Uri uri, Context context) { if (uri == null) { return null; } String[] projection = {MediaStore.Images.Media.DATA}; Cursor cursor = context.getContentResolver().query(uri, projection, null, null, null); if (cursor != null) { int column_index = cursor .getColumnIndexOrThrow(MediaStore.Images.Media.DATA); cursor.moveToFirst(); String path = cursor.getString(column_index); cursor.close(); return path; } return uri.getPath(); }
private void MakeCursor() { String[] cols = new String[]{ MediaStore.Video.Media._ID, MediaStore.Video.Media.TITLE, MediaStore.Video.Media.DATA, MediaStore.Video.Media.MIME_TYPE, MediaStore.Video.Media.ARTIST }; ContentResolver resolver = getActivity().getContentResolver(); if (resolver == null) { System.out.println("resolver = null"); } else { StringBuffer select = new StringBuffer(""); // 查询语句:检索出.mp3为后缀名,时长大于1分钟,文件大小大于1MB的媒体文件 /*if(sp.getFilterSize()) { select.append(" and " + Media.SIZE + " > " + FILTER_SIZE); }*/ if (settingUtil.isFilterVideo()) { select.append(" and " + Media.DURATION + " > " + Constant.FILTER_DURATION); } mSortOrder = MediaStore.Video.Media.TITLE + " COLLATE UNICODE"; mWhereClause = MediaStore.Video.Media.TITLE + " != ''"; mCursor = resolver.query(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, cols, mWhereClause + select, null, mSortOrder); } }
/** * Set Ringtone * * @param context * @param path */ private static void setRingtone(Context context, String path) { if (path == null) { return; } File file = new File(path); ContentValues contentValues = new ContentValues(); contentValues.put(MediaColumns.DATA, file.getAbsolutePath()); String filterName = path.substring(path.lastIndexOf("/") + 1); contentValues.put(MediaColumns.TITLE, filterName); contentValues.put(MediaColumns.MIME_TYPE, "audio/mp3"); contentValues.put(MediaColumns.SIZE, file.length()); contentValues.put(Media.IS_RINGTONE, true); Uri uri = MediaStore.Audio.Media.getContentUriForPath(path); Cursor cursor = context.getContentResolver().query(uri, null, MediaStore.MediaColumns.DATA + "=?", new String[]{path}, null); if (cursor != null && cursor.moveToFirst() && cursor.getCount() > 0) { String id = cursor.getString(0); contentValues.put(MediaStore.Audio.Media.IS_RINGTONE, true); context.getContentResolver().update(uri, contentValues, MediaStore.MediaColumns.DATA + "=?", new String[]{path}); Uri newuri = ContentUris.withAppendedId(uri, Long.valueOf(id)); try { RingtoneManager.setActualDefaultRingtoneUri(context, RingtoneManager.TYPE_RINGTONE, newuri); } catch (Throwable t) { t.printStackTrace(); } cursor.close(); } }
@NonNull public static Intent imagePicker(Context context) { if (permissionManager.isExternalReadStorageGranted(context)) { Intent chooser = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI); chooser.setType("image/*"); Intent intent = Intent.createChooser(chooser, "Choose image"); return intent; } else { Toast.makeText(context, "Permission grant failed", Toast.LENGTH_SHORT).show(); return null; } }
@Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { super.onItemClick(parent, view, position, id); songsView.query(Media.EXTERNAL_CONTENT_URI, AlbumColumns.ALBUM_KEY + " = '" + getLastSelectedName() + "'"); getFlipper().setInAnimation(getContext(), R.anim.slide_in_left); getFlipper().setOutAnimation(getContext(), R.anim.slide_out_left); getFlipper().showNext(); }
private void fillDataToListView() { playList = new ArrayList<String>(); String[] fromColumns = new String[]{MediaStore.Video.Media.TITLE}; int[] toLayoutIDs = new int[]{R.id.media_row_name}; //Cursor cursor = readDataFromSD(getActivity()); SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(), R.layout.dt_video_item, mCursor, fromColumns, toLayoutIDs, 0); video_listview.setAdapter(adapter); while (mCursor.moveToNext()) { //Log.i(TAG, "mine-type is:"+mCursor.getString(mCursor.getColumnIndex(MediaStore.Video.Media.MIME_TYPE))); playList.add(mCursor.getString(mCursor.getColumnIndex(MediaStore.Video.Media.DATA))); } }
private Cursor readDataFromSD(Context context) { Log.d(TAG, "scanFile"); String[] str = new String[]{MediaStore.Audio.Media._ID, MediaStore.Audio.Media.DISPLAY_NAME, MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.DURATION, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.ALBUM, MediaStore.Audio.Media.YEAR, MediaStore.Audio.Media.MIME_TYPE, MediaStore.Audio.Media.SIZE, MediaStore.Audio.Media.DATA}; StringBuffer select = new StringBuffer(""); // 查询语句:检索出.mp3为后缀名,时长大于1分钟,文件大小大于1MB的媒体文件 /*if(sp.getFilterSize()) { select.append(" and " + Media.SIZE + " > " + FILTER_SIZE); }*/ if (settingUtil.isFilterAudio()) { select.append(" and " + Media.DURATION + " > " + Constant.FILTER_DURATION); } /*if (!TextUtils.isEmpty(selections)) { select.append(selections); }*/ Cursor c = MusicUtils.query(context, MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, str, MediaStore.Audio.Media.IS_MUSIC + "=1" + select, null, MediaStore.Audio.Media.TITLE_KEY); return c; }
private void initFillData() { playList = new ArrayList<String>(); String[] fromColumns = new String[]{MediaStore.Audio.Media.TITLE, MediaStore.Audio.Media.ARTIST, MediaStore.Audio.Media.DATA}; int[] toLayoutIDs = new int[]{R.id.media_row_name, R.id.media_row_artist, R.id.media_row_uri}; mCursor = readDataFromSD(getActivity()); SimpleCursorAdapter adapter = new SimpleCursorAdapter(getActivity(), R.layout.dt_audio_item, mCursor, fromColumns, toLayoutIDs, 0); audio_listview.setAdapter(adapter); while (mCursor.moveToNext()) { playList.add(mCursor.getString(mCursor.getColumnIndex(MediaStore.Audio.Media.DATA))); } //mCursor.close(); }
private MusicLoader(ContentResolver contentResolver) { this.contentResolver = contentResolver; this.contentUri = Media.EXTERNAL_CONTENT_URI; this.musicColumns = new String[]{Media._ID, Media.TITLE, Media.ARTIST, Media.DATA, Media.DURATION, Media.SIZE, Media.ALBUM, Media.ALBUM_ID}; this.executor = Executors.newFixedThreadPool(1); }
private List<Music> getMusicList() { List<Music> musicList = new ArrayList(); MusicLoader.this.cursor = contentResolver.query(MusicLoader.this.contentUri, MusicLoader.this.musicColumns, null, null, null); if(cursor == null) { Log.w(TAG, "Music Loader cursor is null (on line 62)"); } else if (!MusicLoader.this.cursor.moveToFirst()) { Log.w(TAG, "Music Loader cursor moveToFirst return false (on line 65)"); } else { int idCol = MusicLoader.this.cursor.getColumnIndex(Media._ID); int displayNameCol = MusicLoader.this.cursor.getColumnIndex(Media.TITLE); int artistCol = MusicLoader.this.cursor.getColumnIndex(Media.ARTIST); int urlCol = MusicLoader.this.cursor.getColumnIndex(Media.DATA); int durationCol = MusicLoader.this.cursor.getColumnIndex(Media.DURATION); int sizeCol = MusicLoader.this.cursor.getColumnIndex(Media.SIZE); int albumCol = MusicLoader.this.cursor.getColumnIndex(Media.ALBUM); int albumIdCol = MusicLoader.this.cursor.getColumnIndex(Media.ALBUM_ID); do { String title = MusicLoader.this.cursor.getString(displayNameCol); long id = MusicLoader.this.cursor.getLong(idCol); long duration = MusicLoader.this.cursor.getLong(durationCol); if(MusicLoader.this.minMusicDuration <= duration) { long size = MusicLoader.this.cursor.getLong(sizeCol); String artist = MusicLoader.this.cursor.getString(artistCol); String url = MusicLoader.this.cursor.getString(urlCol); String album = MusicLoader.this.cursor.getString(albumCol); long albumId = MusicLoader.this.cursor.getLong(albumIdCol); Music music = new Music(id, title); music.setSong(music.getTitle()); music.setArtist(artist); music.setUrl(url); music.setAlbum(album); music.setAlbumId(albumId); music.setDuration(duration); music.setSize(size); musicList.add(music); onProgressUpdate(music); } } while(MusicLoader.this.cursor.moveToNext()); } MusicLoader.this.close(); return musicList; }
/** * Edit Song Tags * * @param context * @param song * @return */ public static boolean editSongTags(Context context, Song song) { File f = new File(song.getmSongPath()); if (f.exists()) { try { AudioFile audioFile = AudioFileIO.read(f); if (audioFile == null) { return false; } TagOptionSingleton.getInstance().setAndroid(true); Tag tag = audioFile.getTag(); if (tag == null) { return false; } String year = song.getYear(); String title = song.getTitle(); String album = song.getAlbum(); String artist = song.getArtist(); String lyrics = song.getLyrics(); tag.deleteField(FieldKey.LYRICS); tag.setField(FieldKey.LYRICS, Html.fromHtml(lyrics).toString()); ContentValues values = new ContentValues(); if (title != null){ tag.setField(FieldKey.TITLE, title); values.put(MediaStore.Audio.Media.TITLE, title); } if (artist != null){ tag.setField(FieldKey.ARTIST, artist); values.put(MediaStore.Audio.Media.ARTIST, artist); } if (album != null){ tag.setField(FieldKey.ALBUM, album); Cursor cursor = context.getContentResolver().query(MediaStore.Audio.Albums.EXTERNAL_CONTENT_URI, new String[]{BaseColumns._ID, MediaStore.Audio.AlbumColumns.ALBUM, MediaStore.Audio.AlbumColumns.ALBUM_KEY, MediaStore.Audio.AlbumColumns.ARTIST}, MediaStore.Audio.AlbumColumns.ALBUM + " = ?", new String[]{album}, MediaStore.Audio.Albums.DEFAULT_SORT_ORDER); if (cursor != null && cursor.moveToFirst()) { long id = cursor.getLong(cursor.getColumnIndex(BaseColumns._ID)); values.put(MediaStore.Audio.Media.ALBUM_ID, id); cursor.close(); } else { values.put(MediaStore.Audio.Media.ALBUM, album); } } if (song.getTrackNumber() != -1){ tag.setField(FieldKey.TRACK, String.valueOf(song.getTrackNumber())); values.put(MediaStore.Audio.Media.TRACK, song.getTrackNumber()); } if (year != null && year.length() > 0){ tag.setField(FieldKey.YEAR, "" + year); values.put(MediaStore.Audio.Media.YEAR, year); } if (values.size() > 0) { context.getContentResolver().update(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, values, android.provider.MediaStore.Audio.Media._ID + "=?", new String[]{String.valueOf(song.getId())}); }else { return false; } audioFile.setTag(tag); AudioFileIO.write(audioFile); } catch (CannotReadException | CannotWriteException | InvalidAudioFrameException | TagException | IOException | ReadOnlyFileException e) { e.printStackTrace(); } return true; } else { return false; } }
public static Song getSongData(String sortOrder, @NonNull Context context, String path) { if (path == null) { return null; } Song song = new Song(); if (PermissionChecker.checkCallingOrSelfPermission(context, Manifest.permission.READ_EXTERNAL_STORAGE) == PermissionChecker.PERMISSION_GRANTED) { Cursor cursor = context.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, null, MediaStore.Audio.Media.DATA + " like ? ", new String[]{"%" + path + "%"}, sortOrder); if (cursor != null && cursor.moveToFirst()) { int idCol = cursor.getColumnIndex(MediaStore.Audio.Media._ID); int titleCol = cursor.getColumnIndex(MediaStore.Audio.Media.TITLE); int artistCol = cursor.getColumnIndex(MediaStore.Audio.Media.ARTIST); int albumCol = cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM); int albumIdCol = cursor.getColumnIndex(MediaStore.Audio.Media.ALBUM_ID); int trackCol = cursor.getColumnIndex(MediaStore.Audio.Media.TRACK); int datacol = cursor.getColumnIndex(MediaStore.Audio.Media.DATA); /** * @return songs metadata */ long id = cursor.getLong(idCol); String title = cursor.getString(titleCol); String artist = cursor.getString(artistCol); String album = cursor.getString(albumCol); long albumId = cursor.getLong(albumIdCol); int track = cursor.getInt(trackCol); String mSongPath = cursor.getString(datacol); song.setAlbum(album); song.setmSongPath(mSongPath); song.setArtist(artist); song.setId(id); song.setAlbumId(albumId); song.setTrackNumber(track); song.setTitle(title); } if (cursor != null) { cursor.close(); } } else { Log.e("DefaultSongLoader", "No read permissions"); } return song; }