我们如何才能从res / raw / song.mp3下载mp3文件并将其保存在本地存储中而不是SdCard中,我进行了很多搜索,但找不到与此相关的任何解决方案
Download is not required for this work ! , to do this , we use copy method ! [ copy file from raw folder to local storage ] :
public class MainActivity extends Activity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Your song or file [in raw folder]
YourFile(R.raw.song);
}
// Start saving the file in external storage ...
private void YourFile(int Resource){
String pathSDCard = Environment.getExternalStorageDirectory() +
"/song.mp3"; // File path
try{
InputStream in = getResources().openRawResource(Resource);
FileOutputStream out = null;
out = new FileOutputStream(pathSDCard);
byte[] buff = new byte[1024];
int read = 0;
try {
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
}
} finally {
in.close();
out.close();
end();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private void end () {
Toast.makeText(MainActivity.this,"File saved in /storage/emulated/0/song.mp3",Toast.LENGTH_LONG).show();
}
}
And permissions :
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.