我尝试在平板电脑上创建目录并希望看到它。
我用这段代码创建目录
public void createDirectory(String sDirectoryName) {
File direct = getDir(sDirectoryName, Context.MODE_PRIVATE);
File fileWithinMyDir = new File(direct, "myfile");
if(!direct.exist()) {
System.out.println("Directory created");
}
else {
System.out.println("Directory not created");
}
}
我看到每次创建目录,但是当我在文件系统中搜索文件夹时,我看不到它。我怎样才能让它可见我做错了吗?
编辑:
我对清单给予了所有许可。我也试过这个代码
File direct = new File(Environment.getExternalStorageDirectory()+"/"+sDirectoryName);
if(!direct.exists())
{
if(direct.mkdir())
{
System.out.println("Directory created");
Toast.makeText(MainActivity.this, "Directory created", Toast.LENGTH_LONG).show();
}
else
{
System.out.println("Directory not created");
Toast.makeText(MainActivity.this, "Directory not created", Toast.LENGTH_LONG).show();
}
}
但这对我也不起作用。
编辑: 为了刷新我使用此代码。
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
的工作。
答案 0 :(得分:0)
创建目录:
if(!direct.exist()) {
if (direct.mkdir())
Log.i(TAG, "Directory created");
else
Log.w(TAG, "Failed to create directory");
}
并且不要忘记清单文件中的权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
答案 1 :(得分:0)
如果文件不存在,则文件不会创建。它只存储它的路径。你的if语句显示它不存在。
答案 2 :(得分:0)
试试这个......
public void createDirectory(String sDirectoryName)
{
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath(), sDirectoryName);
if (!file.exists()) {
file.mkdirs();
}
}
答案 3 :(得分:0)
使用以下代码创建目录。
public void createDirectory(String sDirectoryName) {
File direct = getDir(sDirectoryName, Context.MODE_PRIVATE);
File fileWithinMyDir = new File(direct, "myfile");
if(!direct.exist()) {
direct.mkdirs();
System.out.println("Directory created");
}
else {
System.out.println("Directory not created");
}
}
在AndroidManifest.xml中添加权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
答案 4 :(得分:0)
确保您的表现形式具有以下权限
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
在代码中
File directory = new File(Environment.getExternalStorageDirectory()+DOC_FOLDER_NAME);
// create directory if not exists
if(!directory.exists())
{
if(directory.mkdirs()) //directory is created;
Log.i(" download ","App dir created");
else
Log.w(" download ","Unable to create app dir!");
}
答案 5 :(得分:0)
添加:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
这对于java文件:
File myDirectory = new File(Environment.getExternalStorageDirectory(), "dirName");
if(!myDirectory.exists()) {
myDirectory.mkdirs();
}
删除它:
myDirectory.delete();
这对于父目录的File对象:
//create a File object for the parent directory
File wallpaperDirectory = new File("/sdcard/Wallpaper/");
// have the object build the directory structure, if needed.
wallpaperDirectory.mkdirs();
// create a File object for the output file
File outputFile = new File(wallpaperDirectory, filename);
// now attach the OutputStream to the file object, instead of a String representation
FileOutputStream fos = new FileOutputStream(outputFile);
答案 6 :(得分:0)
注意:因为Android使用MTP协议进行USB连接,有时文件或文件夹不会显示,因为所有内容都已缓存,可能需要刷新。
答案 7 :(得分:0)
您的打印声明令人困惑:
if(!direct.exist()) { // If directory does not exist
System.out.println("Directory created"); // Directory created not true
}
只是创建一个File
对象,它不会创建代码应该是的目录:
if(!direct.exist()) { // If directory does not exist
direct.mkdir(); // Create directory
System.out.println("Directory created");
}
else {
System.out.println("Directory not created");
}
还要确保在您的应用中添加android.permission.WRITE_EXTERNAL_STORAGE
权限。
此外,它建议不要在Android中使用System.out.println
作为模拟器,大多数设备System.out.println
都会重定向到LogCat并使用Log.i()
进行打印。对于非常旧的或自定义的Android版本,这可能不适用。
答案 8 :(得分:0)
问题不在线
File direct = getDir(sDirectoryName, Context.MODE_PRIVATE);
根据文档上下文.MODE_PRIVATE只会在应用程序本身中显示,但是其他程序或用户ID无法找到它。
尝试:
File direct = getDir(sDirectoryName, Context.MODE_WORLD_READABLE);
或
File direct = getDir(sDirectoryName, Context.MODE_WORLD_WRITEABLE);
http://developer.android.com/reference/android/content/Context.html