简体   繁体   English

我们如何从res / raw / song.mp3下载mp3文件并将其存储在本地存储中而不是SdCard中

[英]How can we download mp3 file from res/raw/song.mp3 and store in local Storage not SdCard

我们如何才能从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" />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM