1. 程式人生 > >Android獲取SDcard目錄及建立資料夾;

Android獲取SDcard目錄及建立資料夾;

  • 獲取sdcard目錄
 public static String getSDPath() {
        File sdDir = null;
        boolean sdCardExist = Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);// 判斷sd卡是否存在
        if (sdCardExist) {
            sdDir = Environment.getExternalStorageDirectory();// 獲取跟目錄
        }
        return
sdDir.toString(); }
  • 建立目錄,不限目錄層級
public static String mkdirs(String path) {
        String sdcard = getSDPath();
        if (path.indexOf(getSDPath()) == -1) {
            path = sdcard + (path.indexOf("/") == 0 ? "" : "/") + path;
        }
        File destDir = new File(path);
        if (!destDir.exists()) {
            path = makedir(path);
            if
(path == null) { return null; } } return path; } private static String makedir(String path) { String sdPath = getSDPath(); String[] dirs = path.replace(sdPath, "").split("/"); StringBuffer filePath = new StringBuffer(sdPath); for
(String dir : dirs) { if (!"".equals(dir) && !dir.equals(sdPath)) { filePath.append("/").append(dir); File destDir = new File(filePath.toString()); if (!destDir.exists()) { boolean b = destDir.mkdirs(); if (!b) { return null; } } } } return filePath.toString(); }
  • 所需許可權
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- 在sdcard中建立/刪除檔案的許可權 -->
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
  • 顯示本地圖片
ImageView view5 = findView(R.id.imageview2);
view5.setImageBitmap(ImageUtils.getLoacalBitmap("/storage/sdcard1/myimage/20160807.jpg"));

public static Bitmap getLoacalBitmap(String url) {
        try {
             FileInputStream fis = new FileInputStream(url);
             return BitmapFactory.decodeStream(fis);
        } catch (FileNotFoundException e) {
             //這裡應顯示預設圖片,如圖片無法顯示等;從應用資源圖片中選取
             return null;
        }
   }

相關推薦

Android獲取SDcard目錄建立資料

獲取sdcard目錄 public static String getSDPath() { File sdDir = null; boolean sdCard

獲取指定目錄下的資料(包含檔案)並且拷貝到另一指定資料下,刪除指定目錄下的資料(包含檔案)

    /**      * 複製整個資料夾內容      *       * @param oldPath      *  

php 獲取當前目錄和當前資料

<?php /** * PHP獲取路徑或目錄實現 */ //魔術變數,獲取當前檔案的絕對路徑 echo "__FILE__: ========> ".__FILE__; echo PHP_EOL.PHP_EOL; //魔術變數,獲取當前指令碼的目錄 e

圖片轉成base64格式後上傳OSS-Java建立資料

@Test public void testAddUploadFileInfo() throws Exception{ String str = "/9j/4AAQSkZJRgABAQEAeA

linux在指定目錄建立資料的c語言實現

#include <stdio.h> #include <string.h> #include <stdlib.h> #include <sys/stat.h> #include <unistd.h> #inclu

判斷檔案是否存在建立資料

CString strFilePath = "C:\\Documents and Settings\\Administrator\\桌面\\Image Mosaic Result"; /*判斷資料夾是否存在*/ if (!PathFileExists(strFil

Qt 判斷檔案或資料是否存在建立資料

1. 判斷資料夾是不是存在 引數說明: QString fullPath;//資料夾全路徑 /*方法1*/ bool isDirExist(QString fullPath) {     QDir dir(fullPath);     if(dir.exists())     {       return t

如何在指定的目錄建立資料?cocoa: NSFileManager

NSError *error;NSFileManager *manager = [NSFileManager defaultManager];NSString *home = [@"~" stringByExpandingTildeInPath];   //得到主目錄,也就

Python3.6 讀取txt內容建立資料將圖片拷貝至指定資料,並刪除圖片

 讀取txt內容建立資料夾 # 引入模組 import os #建立資料夾 def mkdir(path): # 去除首位空格 path = path.strip() # 去除尾部 \ 符號 path = path.rstrip("\\")

Nodejs入門基礎(使用fs建立資料檔案並獲取資訊返回頁面)

測試直接ping直接設定的埠號http://localhost:埠號參考程式碼: var fs = require("fs"); var http = require("http"); var url = require("url"); http.createServer(function(r

android 建立檔案和建立資料、將assets下檔案複製到指定目錄

1.獲取APP當前目錄路徑: public String getPath(){ File fileDire = getFilesDir();//獲取../data/應用的包名/fil

Android 建立資料目錄

在根目錄建立一個名為”mypath”的資料夾。 // 定義資料夾目錄地址 final String path = Environment.getExternalStorageDirectory().g

android檔案快取SD卡建立資料失敗解決和bitmap記憶體溢位解決

 1.相關程式碼:    新增許可權: <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:na

QTemporaryDirQTemporaryFile建立臨時目錄文件(創建一個隨機名稱的目錄或文件,兩者均能保證不會覆蓋已有文件)

接口 函數 dir 說明 文件的 removes pen cto path 版權聲明:若無來源註明,Techie亮博客文章均為原創。 轉載請以鏈接形式標明本文標題和地址:本文標題:QTemporaryDir及QTemporaryFile建立臨時目錄及文件夾 本文地址

Unity進階----DoTween工程資料建立(2018/11/12)

DoTween 僅介紹部分常用用法,程式碼參上:(其它操作見官網:http://dotween.demigiant.com/documentation.php) using System.Collections; using System.Collections.Generic; using Uni

用python批量獲取某路徑資料資料下的指定型別檔案,並按原資料結構批量儲存處理後的檔案

因為是把自己成功執行的整個程式碼按幾部分截取出來的,所以每一小節程式碼不一定能單獨執行,特此說明。 1.獲取某路徑資料夾及子資料夾下的指定pcm型別檔案的全部路徑 import os def eachfile(filepath):     pathdi

檔案目錄,遍歷資料檔案屬性

目錄檔案:     1、建立目錄:         注:目錄需要執行的許可權,建立資料夾mode& ~umask         &nbs

使用檔案佇列獲取指定目錄子目錄下的檔案和文價

package cn.itcast.day01_03; import java.awt.List; import java.io.File; import java.util.LinkedList /* * 使用檔案佇列獲取指定目錄及子目錄下的檔案和問價夾 */ public clas

android 6.0 建立資料

在AndroidManifest.xml中新增 <!--在SD卡中建立與刪除檔案許可權--> <uses-permission android:name="android.permission.MOUNT_FORMAT_FILESYSTEMS"/>  

android建立資料和修改其許可權的方法

    /**     * File creation mode: the default mode, where the created file can only     * be accessed by the calling application (or all applications shari