java - Android保存到外部SD卡?

标签 java android sd-card

为什么我无法将此文件保存到外部 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();
        }

  }

最佳答案

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

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

确保将它们放在顶级 <manifest> 中标签,而不是 <application>指定您的 Activity 的标签。

关于java - Android保存到外部SD卡?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5837300/

相关文章:

c# - 如何通过 C# 代码在 Android 上的 Unity3D 中创建图像缩略图?

android - 操作栏,如果单击某个选项卡如何隐藏操作栏(该选项卡单击全屏)

java - 下载的图像无法保存在 SD 卡路径中

android - 怎么把网站的文件存到sd卡

java - 尝试从输入中删除空的新行(使用扫描仪)

java - Android 中取消缩短 URL

java - JSF SelectOneRadio 按钮无法禁用

android - 使用 Processing IDE 进行文本转语音

安卓 : Saving Files to SD Card

java - Spring AOP 注释不起作用