Android 保存到外部 SD 卡吗?

发布于 2024-11-04 16:44:16 字数 1769 浏览 6 评论 0原文

为什么我无法将此文件保存到外部 SD 中?你能检查一下我的代码吗?

 public void Download()
  {
      try {
            //this is the file you want to download from the remote server
            String path ="http://mozilla.cdn.leaseweb.com/firefox/releases/4.0.1/win32/en-US/Firefox%20Setup%204.0.1.exe";
            //this is the name of the local file you will create
            String targetFileName;
                boolean eof = false;
            URL u = new URL(path);
            HttpURLConnection c = (HttpURLConnection) u.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();
            
            //String svto = Environment.getExternalStorageState().toString();
            File path1 = Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES);

            path1.mkdirs();
            FileOutputStream f = new FileOutputStream(new File(path1+"/fox.exe"));
                InputStream in = c.getInputStream();
                byte[] buffer = new byte[1024];
                int len1 = 0;
                while ( (len1 = in.read(buffer)) !=  -1 ) {
                f.write(buffer,0, len1);
                         }
            f.close();
            } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            } catch (ProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

  }

Why I cannot save this file to the external sd? Can you check over my code?

 public void Download()
  {
      try {
            //this is the file you want to download from the remote server
            String path ="http://mozilla.cdn.leaseweb.com/firefox/releases/4.0.1/win32/en-US/Firefox%20Setup%204.0.1.exe";
            //this is the name of the local file you will create
            String targetFileName;
                boolean eof = false;
            URL u = new URL(path);
            HttpURLConnection c = (HttpURLConnection) u.openConnection();
            c.setRequestMethod("GET");
            c.setDoOutput(true);
            c.connect();
            
            //String svto = Environment.getExternalStorageState().toString();
            File path1 = Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES);

            path1.mkdirs();
            FileOutputStream f = new FileOutputStream(new File(path1+"/fox.exe"));
                InputStream in = c.getInputStream();
                byte[] buffer = new byte[1024];
                int len1 = 0;
                while ( (len1 = in.read(buffer)) !=  -1 ) {
                f.write(buffer,0, len1);
                         }
            f.close();
            } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            } catch (ProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不弃不离 2024-11-11 16:44:16

如果您的 AndroidManifest.xml 文件中没有以下权限,此操作将会失败:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />

确保将它们放在顶级 标记中,不是指定您的活动的 标记。

This operation will fail if you don't have the following permissions in your AndroidManifest.xml file:

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.INTERNET" />

Make sure to put them within the top-level <manifest> tag, not the <application> tag where your activities are specified.

天涯离梦残月幽梦 2024-11-11 16:44:16

我之前也遇到过类似的问题。事实证明,我必须先在 SD 卡上手动创建目录,然后再调用诸如 Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); 之类的

代码检查文件资源管理器上是否存在该目录的绝对路径。如果没有,请通过 adb shell 创建它,然后重试。这解决了我的问题,但不一定能解决您的问题,请尝试一下!

I had a problem similar to this before. It turned out that I had to manually create the directory first on the SD card before calling code such as Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);

Check if the absolute path exists on your file explorer for that directory. If it doesn't, then create it via adb shell and then give it a try again. This solved my problem, but it may not necessarily solve yours, give it a try though!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文