1. 得到存储设备的目录:/SDCARD(一般情况下)

SDPATH=Environment.getExternalStorageDirectory()+"/";

2. 判断SD卡上的文件夹是否存在:通过File对象的exists()方法。

/**
   * 判断文件是否已经存在;
   *
  /
  public boolean checkFileExists(String filepath) {
         File file=new File(SDPATH+filepath);
         return file.exists();
    }

3.在SD卡上创建目录:通过File对象的mkdir()方法实现。

/*
   * 在SD卡上创建目录;
   */
  public File createDIR(String dirpath) {
    File dir=new File(SDPATH+dirpath);
    dir.mkdir();
    return dir;
}

4.在SD卡上创建文件:通过File对象的createNewFile()方法实现。
  /*
   * 在SD卡上创建文件;
   */
     public File createFile(String filepath) throws IOException{
      File file=new File(SDPATH+filepath);
           file.createNewFile();
           return file;
     }

5.将InputStream字节流写入到SD卡文件中
     /**
      * 将一个InputStream中的数据写入至SD卡中
      */
   public File writeStreamToSDCard(String dirpath,String filename,InputStream input) {
             File file = null;
             OutputStream output=null;
              try {
                  //创建目录;
                  createDIR(dirpath);
                  //在创建 的目录上创建文件;
                  file = createFile(dirpath+filename);
                  output=new FileOutputStream(file);
                  byte[]bt=new byte[4*1024];
                  while (input.read(bt)!=-1) {
                     output.write(bt);
                  }
                //刷新缓存,
                  output.flush();
              } catch (IOException e) {
                  e.printStackTrace();
              }
              finally{

try{
              output.close();
                  }
          catch (Exception e) {
                     e.printStackTrace();
                  }
              }

return file;

}

6. 访问的权限:
需在AndroidManifest中加上:
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>

Android开发之SD卡上文件操作的更多相关文章

  1. Android入门开发之SD卡读写操作(转)

    SD卡的读写是我们在开发android 应用程序过程中最常见的操作.下面介绍SD卡的读写操作方式: 1. 获取SD卡的根目录 String  sdCardRoot = Environment.getE ...

  2. Android SD卡上文件

    1. 得到存储设备的目录:/SDCARD(一般情况下) SDPATH=Environment.getExternalStorageDirectory()+"/"; 2. 判断SD卡 ...

  3. android中读取SD卡上的数据

    通过Context的openFileInput或者openFileOutput打开的文件输入输出流是操作应用程序的数据文件夹里的文件,这样存储的大小比较有限,为了更好的存取应用程序的大文件数据,应用程 ...

  4. Android 读写位于SD卡上的sqlite数据库文件错误问题

    09-12 15:24:33.903: W/System.err(19499): java.lang.NullPointerException: Attempt to invoke virtual m ...

  5. android开发之路10(文件的读写)

    1.安卓中文件的数据存储实例(将文件保存到手机自带存储空间中): ①MainActivity.java public class MainActivity extends Activity imple ...

  6. Android_(控件)使用ListView显示Android系统中SD卡的文件列表

    使用ListView显示Android SD卡中的文件列表 父类布局activity_main.xml,子类布局line.xml(一个文件的单独存放) 运行截图: 程序结构: <?xml ver ...

  7. android开发之socket快传文件以及消息返回

    应用场景: 两台android机器:一台自建wifi热点,另一台搜索到,连接该wifi热点.之后再通过socket传递消息,文件等,当服务器端接收到消息之后会返回对应的应答消息: 注意点:接收到消息之 ...

  8. android 操作SD卡上的文件

    (1)说明:操作SD卡上的文件须要增加下面权限  在SD卡上创建和删除文件权限  <uses-permission android:name="android.permission.M ...

  9. android学习笔记47——读写SD卡上的文件

    读写SD卡上的文件 通过Context的openFileInput.openFileOutput来打开文件输入流.输出流时,程序打开的都是应用程序的数据文件夹里的文件,其存储的文件大小可能都比较有限- ...

随机推荐

  1. android 自动化压力测试-monkey 2 获取程序包名

    monkey 1 中我们写到: C:\Users\chenshan>adb shell shell@hwG750-T20:/ $ monkey -p cn.emoney.acg -v 500 说 ...

  2. 手写PE文件(一)

    DOS Header(IMAGE_DOS_HEADER)->64 Byte DOS头部 DOS Stub 112字节 "PE"00(Signature) 4个字节 IMAGE ...

  3. Nsdate的各种常用操作

    // //  NVDate.h // //  Created by Noval Agung Prayogo on 2/5/14. //  Copyright (c) 2014 Noval Agung ...

  4. 添加hive默认配置hiverc

    可以在$HOME中加一个.hiverc文件,并在里面配置hive启动的一些参数. Fro example: http://hadooped.blogspot.com/2013/08/hive-hive ...

  5. Http Url Get请求方式需要对中文参数进行编码

    public static void main(String[] args) { try { String mytext = java.net.URLEncoder.encode("上海南站 ...

  6. Sqli-labs less 15

    Less-15 本关没有错误提示,那么我们只能靠猜测进行注入.这里我直接从源代码中看到了sql语句 @$sql="SELECT username, password FROM users W ...

  7. 13test07;字符排序,去重,三三输出

    #include<iostream> #include<string> using namespace std; void buddle(char*,int);//对输入字符的 ...

  8. NODEjs常见错误检查

    一.没有添加对uncaughtException异常的捕捉处理,最起码也要在其中写个日志记录错误,然后可以调用 process.exit(1); 退出进程. 二.处理函数的回调函数检查,经常忘记在回调 ...

  9. HDU 3833 YY's new problem(换种思路的模拟,防超时)

    题目链接 用p[a]保存的是输入的a在第p[a]个,  然后根据差值查找. #include<stdio.h> #include<string.h> int main() { ...

  10. iScroll.js 用法参考

    本文原文链接:http://www.cnblogs.com/duanhuajian/archive/2013/04/08/3008323.html 概要 iScroll 4 这个版本完全重写了iScr ...