Результат выбора файла:
Код: Выделить всё
ActivityResultCallback activityResult = new ActivityResultCallback() {
@Override
public void onActivityResult(ActivityResult o) {
if (o.getResultCode() == RESULT_OK && o.getData() != null) {
Uri uri = o.getData().getData();
if (uri == null || uri.getPath() == null) return;
try {
File file = new File(uri.getPath());
InputStream stream = getApplicationContext().getContentResolver().openInputStream(uri);
String extension = file.getName().substring(file.getName().lastIndexOf(".")).trim();
if (supportedFormats.contains(extension) && stream != null) {
// copying the file so it works even if the original is deleted
String filename = "custom_notif_sound" + extension;
File outFile = new File(getFilesDir(), filename);
outFile.createNewFile();
FileUtils.copyInputStreamToFile(stream, outFile);
stream.close();
setts.edit().putString("custom_notif_sound", filename)
.putInt("notif_sound", -1).apply();
// "notif_sound" is -1 when custom is selected; 0, 1, 2... when a default is selected
Log.d("qvkrimp", outFile.getAbsolutePath());
} else {
Log.e("qvkrimp", "err2");
}
} catch (Exception e) {
Log.e("qvkrimp", "err1", e);
}
}
}
};
Код: Выделить всё
final MediaPlayer mp;
MediaPlayer mp1;
if (setts.getInt("notif_sound", 0) == -1 && setts.getString("custom_notif_sound", null) != null) {
try {
FileInputStream stream = context.openFileInput(setts.getString("custom_notif_sound", null));
mp1 = new MediaPlayer();
mp1.setDataSource(stream.getFD());
mp1.prepareAsync();
stream.close();
// I ALSO TRIED:
File file = new File(context.getFilesDir(), setts.getString("custom_notif_sound", null));
FileInputStream stream = new FileInputStream(file);
mp1 = new MediaPlayer();
mp1.setDataSource(stream.getFD());
stream.close();
// but the result is the same
// when using file.exists(), it returns true so the saving works fine
} catch (Exception e) {
Log.e("qvkrimp", "FILE NOT FOUND at custom notification " + setts.getString("custom_notif_sound", null));
Log.e("qvkrimp", e.getMessage());
mp1 = MediaPlayer.create(context, R.raw.alertone);
}
} else if (setts.getInt("notif_sound", 0) == 1) {
mp1 = MediaPlayer.create(context, R.raw.alerttwo);
} else { // setts.getInt("notif_sound", 0) == 0 but it works as a "default" if theres a problem with the custom
mp1 = MediaPlayer.create(context, R.raw.alertone);
}
mp = mp1;
mp1.release();
mp1 = null;
Код: Выделить всё
mediaserver E Failed to open file 'android.resource://com.wotrox.qvkrfire/2131820545'. (No such file or directory)
com.android.systemui W error loading sound for android.resource://com.wotrox.qvkrfire/2131820545
java.io.IOException: Prepare failed.: status=0x1
at android.media.MediaPlayer._prepare(Native Method)
at android.media.MediaPlayer.prepare(MediaPlayer.java:1347)
at com.android.systemui.media.NotificationPlayer$CreationAndCompletionThread.run(go/retraceme 93beb7d7d00b428fa28f5dec001c038086b88bfc72edc63eb293d570b7834f39:91)
Я просмотрел Stackoverflow и Google в поисках прошлого 2 дня, но я ничего не нашел по этому поводу.
Я пробовал использовать Uri.parse, используя файл из внешнего хранилища, но ничего не помогало, я всегда получаю одну и ту же ошибку.
Спасибо за любую помощь!
РЕДАКТИРОВАТЬ:
Запуск того же самого кода, того же самого файла и метода из действия работает отлично. MediaPlayer не работает в сервисах, только в активностях?
Подробнее здесь: https://stackoverflow.com/questions/786 ... -directory