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即可,這樣就完成了將文件寫入機身內存中。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM