<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
// ПРОВЕРКА СУЩЕСТВУЕТ ЛИ КАРТА ПАМЯТИ
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
toast1.show();
return false;
}
byte[] buffer = null; // БУФФЕР
// ОТКРЫВАЕМ РЕСУРС
InputStream fIn = getBaseContext().getResources().openRawResource(R.raw.track_1);
if (code == 2) fIn = getBaseContext().getResources().openRawResource(R.raw.track_2);
if (code == 3) fIn = getBaseContext().getResources().openRawResource(R.raw.track_3);
if (code == 4) fIn = getBaseContext().getResources().openRawResource(R.raw.track_4);
if (code == 5) fIn = getBaseContext().getResources().openRawResource(R.raw.track_5);
if (code == 6) fIn = getBaseContext().getResources().openRawResource(R.raw.track_6);
if (code == 7) fIn = getBaseContext().getResources().openRawResource(R.raw.track_7);
if (code == 8) fIn = getBaseContext().getResources().openRawResource(R.raw.track_8);
if (code == 9) fIn = getBaseContext().getResources().openRawResource(R.raw.track_9);
if (code == 10) fIn = getBaseContext().getResources().openRawResource(R.raw.track_10);
if (code == 11) fIn = getBaseContext().getResources().openRawResource(R.raw.track_11);
if (code == 12) fIn = getBaseContext().getResources().openRawResource(R.raw.track_12);
if (code == 13) fIn = getBaseContext().getResources().openRawResource(R.raw.track_13);
if (code == 14) fIn = getBaseContext().getResources().openRawResource(R.raw.track_14);
if (code == 15) fIn = getBaseContext().getResources().openRawResource(R.raw.track_15);
if (code == 16) fIn = getBaseContext().getResources().openRawResource(R.raw.track_16);
if (code == 17) fIn = getBaseContext().getResources().openRawResource(R.raw.track_17);
if (code == 18) fIn = getBaseContext().getResources().openRawResource(R.raw.track_18);
if (code == 19) fIn = getBaseContext().getResources().openRawResource(R.raw.track_19);
if (code == 20) fIn = getBaseContext().getResources().openRawResource(R.raw.track_20);
int size=0;
// ЗАПИСЫВАЕМ РЕСУРС В БУФФЕР
try {
size = fIn.available();
buffer = new byte[size];
fIn.read(buffer);
fIn.close();
} catch (IOException e) {
return false;}
// ИМЯ ПАПКИ
String path = Environment.getExternalStorageDirectory().getPath() +
"/New.Loud.Ringtones/";
// ИМЯ ФАЙЛА
String filename="track_1"+".mp3";
if (code == 2) filename = "track_2"+".mp3";
if (code == 3) filename = "track_3"+".mp3";
if (code == 4) filename = "track_4"+".mp3";
if (code == 5) filename = "track_5"+".mp3";
if (code == 6) filename = "track_6"+".mp3";
if (code == 7) filename = "track_7"+".mp3";
if (code == 8) filename = "track_8"+".mp3";
if (code == 9) filename = "track_9"+".mp3";
if (code == 10) filename = "track_10"+".mp3";
if (code == 11) filename = "track_11"+".mp3";
if (code == 12) filename = "track_12"+".mp3";
if (code == 13) filename = "track_13"+".mp3";
if (code == 14) filename = "track_14"+".mp3";
if (code == 15) filename = "track_15"+".mp3";
if (code == 16) filename = "track_16"+".mp3";
if (code == 17) filename = "track_17"+".mp3";
if (code == 18) filename = "track_18"+".mp3";
if (code == 19) filename = "track_19"+".mp3";
if (code == 20) filename = "track_20"+".mp3";
// ЕСЛИ НЕТ ПАПОК - СОЗДАЕМ
boolean exists = (new File(path)).exists();
if (!exists){new File(path).mkdirs();}
// СОХРАНЯЕМ
FileOutputStream save;
try {
save = new FileOutputStream(path+filename);
save.write(buffer);
save.flush();
save.close();
} catch (FileNotFoundException e) {
return false;
} catch (IOException e) {return false;}
Android M
permissions нужно также проверять во время выполнения.public static final int NUMBER_OF_REQUEST = 23401;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
int canRead = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
int canWrite = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (canRead != PackageManager.PERMISSION_GRANTED || canWrite != PackageManager.PERMISSION_GRANTED) {
//Нужно ли нам показывать объяснения , зачем нам нужно это разрешение
if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
//показываем объяснение
} else {
//просим разрешение
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE}, NUMBER_OF_REQUEST);
}
} else {
//ваш код
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case NUMBER_OF_REQUEST: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.e("TAG", "Пользователь дал разрешение");
} else {
Log.e("TAG", "Пользователь отклонил разрешение");
}
return;
}
}
}