Android的保存到外部SD卡吗? [英] Android saving to external SD card?

查看:153
本文介绍了Android的保存到外部SD卡吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨谁能告诉我为什么我不能将此文件保存到外置SD?你可以检查过我的code?

 公共无效下载()
  {
      尝试{
            //这是你想从远程服务器下载文件
            字符串路径=htt​​p://mozilla.cdn.leaseweb.com/firefox/releases/4.0.1/win32/en-US/Firefox%20Setup%204.0.1.exe;
            //这是本地文件,您将创建的名​​称
            串targetFileName;
                布尔EOF = ​​FALSE;
            URL U =新的URL(路径);
            HttpURLConnection的C =(HttpURLConnection类)u.openConnection();
            c.setRequestMethod(GET);
            c.setDoOutput(真);
            c.connect();            //字符串svto = Environment.getExternalStorageState()的toString()。
            文件路径1 = Environment.getExternalStoragePublicDirectory(
                    Environment.DIRECTORY_PICTURES);            path1.mkdirs();
            FileOutputStream中F =新的FileOutputStream(新文件(路径1 +/ fox.exe));
                在的InputStream = c.getInputStream();
                字节[]缓冲区=新的字节[1024];
                INT LEN1 = 0;
                而((LEN1 = in.read(缓冲液))!= - 1){
                f.write(缓冲液,0,LEN1);
                         }
            f.close();
            }赶上(MalformedURLException的E){
            // TODO自动生成catch块
            e.printStackTrace();
            }赶上(的ProtocolException E){
            // TODO自动生成catch块
            e.printStackTrace();
            }赶上(FileNotFoundException异常五){
            // TODO自动生成catch块
            e.printStackTrace();
            }赶上(IOException异常五){
            // TODO自动生成catch块
            e.printStackTrace();
        }  }


解决方案

如果您不具有下列权限在的Andr​​oidManifest.xml 文件,该操作将失败:

 <使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E/>
    <使用许可权的android:NAME =android.permission.INTERNET对/>

请务必把他们的顶级内<清单> 标签,而不是<应用> 其中指定的活动标记。

Hey guys can someone tell me 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();
        }

  }

解决方案

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.

这篇关于Android的保存到外部SD卡吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆