如何在Android中的SDCard中创建文件夹
题
我想在我的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() + "");
}
}
}
.
此外,我已设置SDCard Write权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
.
我已经运行了多次应用程序,每次我得到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
.
我已经阅读了类似的问题,但没有什么有用的。我该怎么办才能获得代码运行?我的问题是什么?
解决方案 5
我不知道为什么应用程序采取行动,但最后我通过删除和重置android清单文件中的sdcard写权限来克服此问题。谢谢你们所有人的帮助,并为您的时间而道歉。
其他提示
编辑
试试:
File mydir = new File(Environment.getExternalStorageDirectory() + "/mydir/");
if(!mydir.exists())
mydir.mkdirs();
else
Log.d("error", "dir. already exists");
.
并重新检查权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
. 这是我在图片文件夹OD SDCard中创建文件夹MyDir的方式:
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"MyDir");
mediaStorageDir.mkdirs();
. 以这种方式:
File file=new File(Environment.getExternalStorageDirectory() + File.separator +"MyApp/");
file.mkdirs();
. 试试这个代码:),它简单。
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);
. 不隶属于 StackOverflow