Я пытаюсь загрузить файл MP3 из Интернета с помощью потока и сохранить его в папку с рингтонами, используя этот метод.
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES)
Я добавил необходимые разрешения:
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.write_external_storage"/>
Когда я проверяю свое приложение на эмуляторе (в настоящее время я использую Andy), приложение работает отлично. Когда я проверяю это на реальном устройстве (более одного), я не получаю разрешения: /storage/sdcard0/Ringtones/MyRingtone.mp3: open failed: EACCES (разрешение отклонено)
Кто-нибудь знает, чего не хватает?
Вот полный метод загрузки:
protected String doInBackground(String... f_url) {
Log.d(TAG, "doInBackground.Started");
int count;
try {
mDownloadSuccess = true;
URL url = new URL(f_url[0]);
URLConnection connection = url.openConnection();
connection.connect();
// this will be useful so that you can show a typical 0-100%
// progress bar
int lengthOfFile = connection.getContentLength();
// download the file
InputStream input = new BufferedInputStream(url.openStream(),
8192);
// Output stream
File path = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_RINGTONES);
File file = new File(path, mModel.getTitle() + ".mp3");
OutputStream output = new FileOutputStream(file);
byte data[] = new byte[1024];
long total = 0;
while ((count = input.read(data)) != -1) {
total += count;
// publishing the progress....
// After this onProgressUpdate will be called
publishProgress("" + (int) ((total * 100) / lengthOfFile));
// writing data to file
output.write(data, 0, count);
}
// flushing output
output.flush();
// closing streams
output.close();
input.close();
} catch (Exception e) {
Log.e("Error: ", e.getMessage());
mDownloadSuccess = false;
}
return null;
}
Спасибо