zoukankan      html  css  js  c++  java
  • Android开发之sd卡存储和机身存储的路径获取

    来源:https://blog.csdn.net/anjingshuai/article/details/84682779

    开发过程中碰到将文件存储到手机中时,要先判断是否有sd卡,如下所示

    1.  
      // 判断是否有SD卡
    2.  
      private static boolean ExistSDCard() {
    3.  
      if (android.os.Environment.getExternalStorageState().equals(
    4.  
      android.os.Environment.MEDIA_MOUNTED)) {
    5.  
      return true;
    6.  
      } else
    7.  
      return false;
    8.  
      }

     如果存在,则要获取sd卡的根目录路径,在目录下创建新的文件夹,sd卡根目录路径如下:

    1.  
      public static String SDCARDPATH = Environment.getExternalStorageDirectory()
    2.  
      .getPath();

    然后是将要复制的文件写到sd卡下新建的文件夹内,代码如下:

    1.  
      private void copyzipfileToLocalDir(final String path, final String filename) {
    2.  
      File file = new File(path);
    3.  
      if (file.exists()) {
    4.  
      Uri uri = Uri.fromFile(file);
    5.  
      Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    6.  
      intent.setClass(MainActivity.this, TestActivity.class);
    7.  
      startActivity(intent);
    8.  
      return;
    9.  
      }
    10.  
      pdlog = new ProgressDialog(this);
    11.  
      pdlog.setMessage("正在复制文件...");
    12.  
      pdlog.show();
    13.  
       
    14.  
      new Thread() {
    15.  
      public void run() {
    16.  
      try {
    17.  
      InputStream input = getApplicationContext().getAssets()
    18.  
      .open(filename);
    19.  
      File f = new File(path);
    20.  
      if (f.exists()) {
    21.  
      return;
    22.  
      }
    23.  
      File file = f.getParentFile();
    24.  
      // SDCARD/CN/ZNsql====================path
    25.  
      if (!file.exists()) {
    26.  
      file.mkdir();
    27.  
      }
    28.  
      FileOutputStream fout = new FileOutputStream(f);
    29.  
      byte[] buff = new byte[1024];
    30.  
      int len = 0;
    31.  
      while ((len = input.read(buff)) > 0) {
    32.  
      fout.write(buff, 0, len);
    33.  
      }
    34.  
      fout.close();
    35.  
      input.close();
    36.  
       
    37.  
      } catch (Exception e) {
    38.  
      e.printStackTrace();
    39.  
      }
    40.  
      handler.sendEmptyMessage(1);
    41.  
      };
    42.  
      }.start();
    43.  
      }
    44.  
      private Handler handler = new Handler() {
    45.  
       
    46.  
      public void handleMessage(android.os.Message msg) {
    47.  
      switch (msg.what) {
    48.  
      case 1:
    49.  
      if (pdlog != null) {
    50.  
      if (pdlog.isShowing()) {
    51.  
      pdlog.cancel();
    52.  
      }
    53.  
      ;
    54.  
      }
    55.  
      // jump
    56.  
      File file = new File(SDCARDPATH+ "androidtest.pdf");
    57.  
       
    58.  
      if (file.exists()) {
    59.  
      Uri uri = Uri.fromFile(file);
    60.  
      Intent intent = new Intent(Intent.ACTION_VIEW, uri);
    61.  
      intent.setClass(MainActivity.this, TestActivity.class);
    62.  
      startActivity(intent);
    63.  
      }
    64.  
       
    65.  
      break;
    66.  
      default:
    67.  
      break;
    68.  
      }
    69.  
      };
    70.  
      };

    这样就将assets下的文件写入了外置sd卡,对于一些不支持外置存储卡的Android手机,我们可以将文件写入机身内存,也就是俗称的ROM中,RomPath= Environment.getDataDirectory().getPath();当判断到没有外置sd卡时就可以把path换成这个RomPath即可,这样就完成了将文件写入机身内存中。

  • 相关阅读:
    个人笔记 1.3.net 3.5新特性
    个人笔记 1.2.net 2.0新特性
    面试了,web标准能简单说下吗?w3c,,错了
    SEO网络营销热门关键词排名较好的反向链接
    eWebEditor在ie8下上传失效
    个人笔记 1.4.net 3.0新特性
    个人笔记 1.5.http状态值
    MOSS项目开发(5) 会议还是会议
    MOSS项目开发 周记(第二周)
    MOSS项目开发 周记(第五周)
  • 原文地址:https://www.cnblogs.com/LiTZen/p/12188771.html
Copyright © 2011-2022 走看看