Fix: can't play media files from file browser

When media file is selected from various file browers, the
uri sent to app might be different scheme like file or content.
Fix is to query the media file based on different scheme.
This commit is contained in:
Feng Zheng 2017-03-23 20:47:14 -05:00
commit d411b33983
2 changed files with 31 additions and 11 deletions

View file

@ -2,11 +2,14 @@ package com.kabouzeid.gramophone.helper;
import android.app.Activity; import android.app.Activity;
import android.content.ComponentName; import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.Context; import android.content.Context;
import android.content.ContextWrapper; import android.content.ContextWrapper;
import android.content.Intent; import android.content.Intent;
import android.content.ServiceConnection; import android.content.ServiceConnection;
import android.net.Uri;
import android.os.IBinder; import android.os.IBinder;
import android.provider.DocumentsContract;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable; import android.support.annotation.Nullable;
@ -17,6 +20,7 @@ import com.kabouzeid.gramophone.loader.SongLoader;
import com.kabouzeid.gramophone.model.Song; import com.kabouzeid.gramophone.model.Song;
import com.kabouzeid.gramophone.service.MusicService; import com.kabouzeid.gramophone.service.MusicService;
import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Random; import java.util.Random;
import java.util.WeakHashMap; import java.util.WeakHashMap;
@ -374,17 +378,34 @@ public class MusicPlayerRemote {
return -1; return -1;
} }
public static void playFile(String path) { public static void playFromUri(Uri uri) {
if (musicService != null) { if (musicService != null && uri.getScheme() != null) {
ArrayList<Song> songs = SongLoader.getSongs(SongLoader.makeSongCursor( ArrayList<Song> songs = null;
musicService, if (uri.getScheme().equals(ContentResolver.SCHEME_CONTENT)) {
MediaStore.Audio.AudioColumns.DATA + "=?", String songId = null;
new String[]{path} if (uri.getAuthority().equals("com.android.providers.media.documents")) {
)); songId = DocumentsContract.getDocumentId(uri).split(":")[1];
if (!songs.isEmpty()) { } else if (uri.getAuthority().equals("media")) {
songId = uri.getLastPathSegment();
}
if (songId != null) {
songs = SongLoader.getSongs(SongLoader.makeSongCursor(
musicService,
MediaStore.Audio.AudioColumns._ID + "=?",
new String[]{songId}
));
}
} else if (uri.getScheme().equals(ContentResolver.SCHEME_FILE)) {
songs = SongLoader.getSongs(SongLoader.makeSongCursor(
musicService,
MediaStore.Audio.AudioColumns.DATA + "=?",
new String[]{new File(uri.getPath()).getAbsolutePath()}
));
}
if (songs != null && !songs.isEmpty()) {
openQueue(songs, 0, true); openQueue(songs, 0, true);
} else { } else {
//TODO the file is not listed in the media store //TODO uri is not listed in the media store
} }
} }
} }

View file

@ -46,7 +46,6 @@ import com.kabouzeid.gramophone.util.PreferenceUtil;
import com.kabouzeid.gramophone.util.Util; import com.kabouzeid.gramophone.util.Util;
import com.sothree.slidinguppanel.SlidingUpPanelLayout; import com.sothree.slidinguppanel.SlidingUpPanelLayout;
import java.io.File;
import java.util.ArrayList; import java.util.ArrayList;
import butterknife.BindView; import butterknife.BindView;
@ -299,7 +298,7 @@ public class MainActivity extends AbsSlidingMusicPanelActivity {
} }
if (uri != null && uri.toString().length() > 0) { if (uri != null && uri.toString().length() > 0) {
MusicPlayerRemote.playFile(new File(uri.getPath()).getAbsolutePath()); MusicPlayerRemote.playFromUri(uri);
handled = true; handled = true;
} else if (MediaStore.Audio.Playlists.CONTENT_TYPE.equals(mimeType)) { } else if (MediaStore.Audio.Playlists.CONTENT_TYPE.equals(mimeType)) {
final int id = (int) parseIdFromIntent(intent, "playlistId", "playlist"); final int id = (int) parseIdFromIntent(intent, "playlistId", "playlist");