我正在尝试将文件保存到以下位置,
FileOutputStream fos = new FileOutputStream("/sdcard/Wallpaper/"+fileName);
但是却遇到异常,java.io.FileNotFoundException
但是,当我将路径设置为"/sdcard/"
有效时。
现在,我假设无法以这种方式自动创建目录。
有人可以建议如何创建directory and sub-directory
使用代码吗?
我正在尝试将文件保存到以下位置,
FileOutputStream fos = new FileOutputStream("/sdcard/Wallpaper/"+fileName);
但是却遇到异常,java.io.FileNotFoundException
但是,当我将路径设置为"/sdcard/"
有效时。
现在,我假设无法以这种方式自动创建目录。
有人可以建议如何创建directory and sub-directory
使用代码吗?
Answers:
如果创建一个包装顶级目录的File对象,则可以调用它的mkdirs()方法来构建所有需要的目录。就像是:
// 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);
注意:明智的做法是使用Environment.getExternalStorageDirectory()获取“ SD卡”目录,因为如果随身携带的手机带有SD卡以外的其他东西(例如内置闪存,苹果手机)。无论哪种方式,您都应记住,由于SD卡可能已卸下,因此需要检查以确保它确实存在。
更新:从API级别4(1.6)开始,您还必须请求权限。这样的事情(在清单中)应该起作用:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
这是对我有用的。
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
在清单和下面的代码中
public static boolean createDirIfNotExists(String path) {
boolean ret = true;
File file = new File(Environment.getExternalStorageDirectory(), path);
if (!file.exists()) {
if (!file.mkdirs()) {
Log.e("TravellerLog :: ", "Problem creating Image folder");
ret = false;
}
}
return ret;
}
实际上,我使用了@fiXedd解决方案的一部分,它对我有用:
//Create Folder
File folder = new File(Environment.getExternalStorageDirectory().toString()+"/Aqeel/Images");
folder.mkdirs();
//Save the path as a string value
String extStorageDirectory = folder.toString();
//Create New file and name it Image2.PNG
File file = new File(extStorageDirectory, "Image2.PNG");
确保您使用的是mkdirs()而不是mkdir()来创建完整路径
File.mkdirs()
。然后,他展示了一个使用它的示例。
确保存在外部存储:http : //developer.android.com/guide/topics/data/data-storage.html#filesExternal
private boolean isExternalStoragePresent() {
boolean mExternalStorageAvailable = false;
boolean mExternalStorageWriteable = false;
String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
// We can read and write the media
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states, but
// all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}
if (!((mExternalStorageAvailable) && (mExternalStorageWriteable))) {
Toast.makeText(context, "SD card not present", Toast.LENGTH_LONG)
.show();
}
return (mExternalStorageAvailable) && (mExternalStorageWriteable);
}
不要忘记确保文件/文件夹名称中没有特殊字符。当我使用变量设置文件夹名称时,出现了“:”
文件/文件夹名称中不允许使用字符
“ * /:<>?\ |
在这种情况下,U可能会发现此代码很有帮助。
以下代码删除了所有“:”并将其替换为“-”
//actualFileName = "qwerty:asdfg:zxcvb" say...
String[] tempFileNames;
String tempFileName ="";
String delimiter = ":";
tempFileNames = actualFileName.split(delimiter);
tempFileName = tempFileNames[0];
for (int j = 1; j < tempFileNames.length; j++){
tempFileName = tempFileName+" - "+tempFileNames[j];
}
File file = new File(Environment.getExternalStorageDirectory(), "/MyApp/"+ tempFileName+ "/");
if (!file.exists()) {
if (!file.mkdirs()) {
Log.e("TravellerLog :: ", "Problem creating Image folder");
}
}
我遇到了同样的问题。Android中有两种类型的权限:
这是在Android 6.0中获取危险权限的策略
onRequestPermissionsResult
这是我的情况:我需要写入外部存储。
首先,请检查我是否具有以下权限:
...
private static final int REQUEST_WRITE_STORAGE = 112;
...
boolean hasPermission = (ContextCompat.checkSelfPermission(activity,
Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED);
if (!hasPermission) {
ActivityCompat.requestPermissions(parentActivity,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
REQUEST_WRITE_STORAGE);
}
然后检查用户的批准:
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode)
{
case REQUEST_WRITE_STORAGE: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
//reload my activity with permission granted or use the features what required the permission
} else
{
Toast.makeText(parentActivity, "The app was not allowed to write to your storage. Hence, it cannot function properly. Please consider granting it this permission", Toast.LENGTH_LONG).show();
}
}
}
}
我遇到了同样的问题,无法在Galaxy S上创建目录,但能够在Nexus和Samsung Droid上成功创建目录。我的解决方法是添加以下代码行:
File dir = new File(Environment.getExternalStorageDirectory().getPath()+"/"+getPackageName()+"/");
dir.mkdirs();
File sdcard = Environment.getExternalStorageDirectory();
File f=new File(sdcard+"/dor");
f.mkdir();
这将在您的SD卡中创建一个名为dor的文件夹。然后为手动插入dor文件夹中的eg- filename.json获取文件。喜欢:
File file1 = new File(sdcard,"/dor/fitness.json");
.......
.....
<users-permission android:name =“ android.permission.WRITE_EXTERNAL_STORAGE” />
并且不要忘记在清单中添加代码
//Create File object for Parent Directory
File wallpaperDir = new File(Environment.getExternalStorageDirectory().getAbsoluteFile() +File.separator + "wallpaper");
if (!wallpaperDir.exists()) {
wallpaperDir.mkdir();
}
File out = new File(wallpaperDir, wallpaperfile);
FileOutputStream outputStream = new FileOutputStream(out);
刚刚完成了Vijay的帖子...
表现
uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"
功能
public static boolean createDirIfNotExists(String path) {
boolean ret = true;
File file = new File(Environment.getExternalStorageDirectory(), path);
if (!file.exists()) {
if (!file.mkdirs()) {
Log.e("TravellerLog :: ", "Problem creating Image folder");
ret = false;
}
}
return ret;
}
用法
createDirIfNotExists("mydir/"); //Create a directory sdcard/mydir
createDirIfNotExists("mydir/myfile") //Create a directory and a file in sdcard/mydir/myfile.txt
您可以检查错误
if(createDirIfNotExists("mydir/")){
//Directory Created Success
}
else{
//Error
}
这将使sdcard中的文件夹具有您提供的文件夹名称。
File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/Folder name");
if (!file.exists()) {
file.mkdirs();
}
ivmage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(
Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(i, RESULT_LOAD_IMAGE_ADD);
}
});`