我正在开发一个应用程序,当SD卡也可用时,我需要在内部存储器(手机存储器)中创建文件夹。我正在使用Environment.getExternalStorageDirectory().toString()+"/"+folderName;
,当我运行这个widout SD卡时,它在内部创建文件夹但是在插入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);
答案 0 :(得分:0)
使用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.
干杯:)
答案 1 :(得分:0)
您可以在内部存储器中创建一个文件夹,如下所示:
File dir = context.getDir("dir", Context.MODE_PRIVATE);
dir.mkdirs();
答案 2 :(得分:0)
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();
希望这有帮助!