簡體   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中,我進行了很多搜索,但找不到與此相關的任何解決方案

這項工作不需要下載! ,為此,我們使用復制方法! [將文件從原始文件夾復制到本地存儲]:

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();
    }


}

權限

 <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