嘿!我想保存在从互联网收到的 ExternalStoragePublic 文件中,但我真的卡住了。你能帮我写代码吗?

Posted

技术标签:

【中文标题】嘿!我想保存在从互联网收到的 ExternalStoragePublic 文件中,但我真的卡住了。你能帮我写代码吗?【英文标题】:Hey! I wanna save in ExternalStoragePublic file which I receive from the internet, but I really stuck. May you help me with code? 【发布时间】:2017-09-11 03:53:18 【问题描述】:
url = "http://r8---sn-03guxaxjvh-3c2r.googlevideo.com/videoplayback?sparams=dur%2Cei%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Clmt%2Cmime%2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Cratebypass%2Csource%2Cexpire&mn=sn-03guxaxjvh-3c2r&ip=212.113.45.145&source=youtube&mm=31&mv=m&mime=video%2Fmp4&mt=1505092537&ipbits=0&initcwndbps=685000&dur=2223.728&id=o-AM9pUI9o5NsL8P-jGi5-w17xJOo-VVQ-TrWlMZaV17cp&key=yt6&lmt=1499875418101464&signature=4AACC08B22F2F1F343F5A044188CD751A6AD2F08.A7BA661DDC07639A7E414169226A35A700888AF3&ms=au&ei=HOS1WezYFZfq7gT40rnoAw&itag=22&pl=22&expire=1505114236&ratebypass=yes&title=Gothic+Rock+-+Dark+Music";


            streamPlayer.setAudiostreamType(AudioManager.STREAM_MUSIC);
            try 
                streamPlayer.setDataSource(url);
             catch (IOException e) 
                e.printStackTrace();
            
            try 
                streamPlayer.prepare();
             catch (IOException e) 
                e.printStackTrace();
            
            streamPlayer.start();`

我的问题是如何在设备上存储我的 streamPlayer 对象?

【问题讨论】:

【参考方案1】:

试试这个

private static void downloadFile(String url, File outputFile) 
  try 
      URL u = new URL(url);
      URLConnection conn = u.openConnection();
      int contentLength = conn.getContentLength();

      DataInputStream stream = new DataInputStream(u.openStream());

        byte[] buffer = new byte[contentLength];
        stream.readFully(buffer);
        stream.close();

        DataOutputStream fos = new DataOutputStream(new                         
        FileOutputStream(outputFile));
        fos.write(buffer);
        fos.flush();
        fos.close();
   catch(FileNotFoundException e) 
  return; // swallow a 404
   catch (IOException e) 
 return; // swallow a 404
  

【讨论】:

以上是关于嘿!我想保存在从互联网收到的 ExternalStoragePublic 文件中,但我真的卡住了。你能帮我写代码吗?的主要内容,如果未能解决你的问题,请参考以下文章

在从 C# 调用的 Matlab 函数中保存状态

离线保存应用评分[关闭]

嘿伙计们,我尝试将生成的 apk 上传到 Google Play 商店并不断收到此错误任何想法

c#将文件保存到桌面

为啥我在从 Java 批量执行 PostgreSQL 存储过程时收到错误消息,提示“未预期结果”?

SwiftUI array.isEmpty 在从 Firestore 收到数组之前不显示