I made a MusicPlayer that plays recursively files in a folder. It is called as usual as a standalone through the LAUNCHER
or when a song is clicked in a filemanager. It works as intended.
Yet, I do not want my MusicPlayer to be called at all times. For example, when clicking a song in, say, WhatsApp, I want the system to call the "usual small thingy". Yet, my app is called. Obviously, too much DEFAULT
...
How can I force my app as DEFAULT
but only when called from a filemanager? Maybe tweaking the category
tags? The scheme
? I tried playing with those without success. I am aware that I cannot predict how ALL filemanagers are going to build their intents... but maybe something can be done? Obviously, an option would be to remove the DEFAULT
altogether, but that is a bit extreme.
This is the relevant part of my AndroidManifest.xml
(intents):
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter tools:ignore="AppLinkUrlError">
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="audio/*" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="audio/*" android:scheme="content" />
</intent-filter>
EDIT (OT) This is the method I use to get the path from the URI:
public String getRealPathFromURI(Context context, Uri contentURI) {
String fileName = contentURI.getPath();
if (fileName != null) {
fileName = fileName.replaceFirst(".*/external_files", sd)
.replaceFirst(".*/primary:",sd+"/")
.replaceFirst(".*/root","")
.replaceAll("%20"," ");
if (new File(fileName).exists()) return fileName;
}
if ("content".equals(contentURI.getScheme())) {
ContentResolver cR = context.getContentResolver();
Cursor cursor = cR.query(contentURI, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
int column_index;
List<String> cols = Arrays.asList(cursor.getColumnNames());
try {
if (cols.contains("_data")) {
column_index = cursor.getColumnIndexOrThrow("_data");
} else if (cols.contains("_display_name")) {
column_index = cursor.getColumnIndexOrThrow("_display_name");
} else
return "FAILED TO RETRIEVE FILENAME";
fileName = Uri.parse(cursor.getString(column_index)).toString().replaceAll("/external_files", sd);
} catch (IllegalArgumentException e) {
e.printStackTrace();
//https://stackoverflow.com/a/48052015/1483390
FileOutputStream outputStream = null;
InputStream inputStream = null;
try {
String tp = MimeTypeMap.getSingleton().getExtensionFromMimeType(cR.getType(contentURI));
File f = File.createTempFile("xvftemp_","."+tp, context.getExternalCacheDir());
f.deleteOnExit();
fileName = f.getAbsolutePath();
inputStream = cR.openInputStream(contentURI);
if (inputStream != null) {
outputStream = new FileOutputStream(f);
byte[] buffer = new byte[1024];
int read;
while ((read = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, read);
}
outputStream.flush();
outputStream.close();
inputStream.close();
}
} catch (IOException e1) {
e1.printStackTrace();
} finally {
try {
if (outputStream != null) outputStream.close();
if (inputStream != null) inputStream.close();
} catch (IOException e2) {
e.printStackTrace();
}
}
}
}
if (cursor != null) cursor.close();
}
return fileName;
}