簡體   English   中英

當存在SD卡(外部存儲)時,如何在Android 4.4的內部存儲中創建文件夾

[英]How to create a folder in internal storage in Android 4.4 when SD card (External storage) is present

我正在開發一個需要在SD卡也可用時在內部存儲器(電話存儲器)中創建文件夾的應用程序。 我正在使用Environment.getExternalStorageDirectory().toString()+"/"+folderName; 當我運行此widout SD卡時,它會在Internal中創建文件夾,但在插入SD卡后,當我運行相同的SD卡時,它會創建文件夾。 但是我想在內部存儲中創建文件夾。

這是我的代碼

    private static File contactsFile;
    private static String contacts_storage_path;

    contacts_storage_path = Environment.getExternalStorageDirectory().toString()+"/"+folderName;
            contactsFile = new File(contacts_storage_path);
            if (!contactsFile.exists())
            {
                 //contactsFile.createNewFile();
                contactsFile.mkdirs();
            } 
             filecontact=new File(contactsFile, contact_file_name);

使用Context.getDir(String value,int mode)在內部存儲中創建目錄很重要。

請參閱以下代碼-

File mydir = context.getDir("mydir", Context.MODE_PRIVATE); //Creating an internal dir;
File fileWithinMyDir = new File(mydir, "myfile"); //Getting a file within the dir.
FileOutputStream out = new FileOutputStream(fileWithinMyDir); //Use the stream as usual to write into the file.

干杯:)

您可以這樣在內部存儲器中創建一個文件夾:

File dir = context.getDir("dir", Context.MODE_PRIVATE);
dir.mkdirs();
FileOutputStream outStream = null;
**File sdCard = Environment.getExternalStorageDirectory();**
**File dir = new File(sdCard.getAbsolutePath() + "/YourFolderName");**
dir.mkdirs();
//to know the path
Log.d(TAG, dir.getAbsolutePath();
String fileName = String.format("%d.jpg", System.currentTimeMillis());
File outFile = new File(dir, fileName);
outStream = new FileOutputStream(outFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();

希望這可以幫助!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM