如何在android中的sdcard中创建文件夹
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21028247/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to create folder in sdcard in android
提问by Ali Allahyar
I want to make folders in my sdcard and I have used the code below:
我想在我的 sdcard 中创建文件夹,我使用了以下代码:
public class Screen extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome);
operateOnFirstUsage();
}
private void operateOnFirstUsage() {
String state = Environment.getExternalStorageState();
Log.d("Media State", state);
if (Environment.MEDIA_MOUNTED.equals(state)) {
File appDirectory = new File(
Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyApp/");
Log.d("appDirectroyExist", appDirectory.exists() + "");
if (!appDirectory.exists())
Log.d("appDir created: ", appDirectory.mkdir() + "");
File dbDirectory = new File(
Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyApp/Database/");
Log.d("dbDirectroyExist", dbDirectory.exists() + "");
if (!dbDirectory.exists())
Log.d("dbDir created: ", dbDirectory.mkdirs() + "");
File themesDirectory = new File(
Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyApp/Themes/");
Log.d("themesDirectroyExist", themesDirectory.exists() + "");
if (!themesDirectory.exists())
Log.d("themesDir created: ", themesDirectory.mkdirs() + "");
}
}
}
Also, I have set the sdcard write permission:
另外,我已经设置了sdcard写权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
I've run the application several times and every time I get the LogCat output:
我已经多次运行该应用程序,每次获得 LogCat 输出时:
01-09 21:38:13.701: D/Media State(15363): mounted
01-09 21:38:13.701: D/appDirectroyExist(15363): false
01-09 21:38:13.701: D/appDir created:(15363): false
01-09 21:38:13.701: D/dbDirectroyExist(15363): false
01-09 21:38:13.701: D/dbDir created:(15363): false
01-09 21:38:13.701: D/themesDirectroyExist(15363): false
01-09 21:38:13.701: D/themesDir created:(15363): false
I have read similar question, but nothing useful to get. What should I do to get the code running? What is my problem?
我读过类似的问题,但没有什么有用的。我应该怎么做才能让代码运行?我的问题是什么?
采纳答案by Ali Allahyar
I don't know why the application acted that way, but finally I overcame this problem by removing and resetting the sdcard write permission in the Android manifest file. Thanks all of you for your kind help and apologize for taking your time.
我不知道为什么应用程序会这样,但最终我通过删除和重置 Android 清单文件中的 sdcard 写入权限来克服这个问题。感谢大家的热心帮助,并为耽误您的时间而道歉。
回答by dakshbhatt21
Edited
已编辑
Try this:
尝试这个:
File mydir = new File(Environment.getExternalStorageDirectory() + "/mydir/");
if(!mydir.exists())
mydir.mkdirs();
else
Log.d("error", "dir. already exists");
And recheck permission
并重新检查权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
回答by gile
This is how I created folder MyDir in the Pictures folder od sdcard:
这就是我在图片文件夹 od sdcard 中创建文件夹 MyDir 的方式:
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"MyDir");
mediaStorageDir.mkdirs();
回答by Dharmendra
Try this way:
试试这个方法:
File file=new File(Environment.getExternalStorageDirectory() + File.separator +"MyApp/");
file.mkdirs();
回答by Agilanbu
Try this code:) , its simple.
试试这个代码:),它很简单。
String fileName = "CallHistory.pdf";
// create a File object for the parent directory
File PDF_Directory = new File("/sdcard/MOBstate/");
// have the object build the directory structure, if needed.
PDF_Directory.mkdirs();
// create a File object for the output file
File outputFile = new File(PDF_Directory, fileName);
// now attach the OutputStream to the file object, instead of a ring representation
FileOutputStream fos = new FileOutputStream(outputFile);