saj*_*joo 55 directory io android file
我一直在尝试以/sdcard编程方式创建一个目录,但它无法正常工作.下面的代码总是输出directory not created.
boolean success = (new File("/sdcard/map")).mkdir();
if (!success) {
Log.i("directory not created", "directory not created");
} else {
Log.i("directory created", "directory created");
}
Run Code Online (Sandbox Code Playgroud)
Gop*_*ath 150
这里有三件事需要考虑:
不要假设SD卡安装在/sdcard(默认情况下可能是真的,但最好不要硬编码.).您可以通过查询系统获取SD卡的位置:
Environment.getExternalStorageDirectory();
Run Code Online (Sandbox Code Playgroud)您必须通过在AndroidManifest.xml文件中添加uses-permission条目来通知Android您的应用程序需要写入外部存储:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Run Code Online (Sandbox Code Playgroud)如果此目录已存在,则mkdir将返回false.因此,请检查目录是否存在,然后尝试创建目录(如果该目录不存在).在您的组件中,使用以下内容:
File folder = new File(Environment.getExternalStorageDirectory() + "/map");
boolean success = true;
if (!folder.exists()) {
success = folder.mkdir();
}
if (success) {
// Do something on success
} else {
// Do something else on failure
}
Run Code Online (Sandbox Code Playgroud)Dii*_*iii 27
将Android手机更新为6.0(API级别23)后,我遇到了同样的问题.以下解决方案适用于我.希望它也能帮到你.
请检查你的Android版本.如果> = 6.0(API级别23),则不仅需要包括
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Run Code Online (Sandbox Code Playgroud)
在你的AndroidManifest.xml中,还要在调用mkdir()之前请求权限.代码snopshot.
public static final int MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE = 1;
public int mkFolder(String folderName){ // make a folder under Environment.DIRECTORY_DCIM
String state = Environment.getExternalStorageState();
if (!Environment.MEDIA_MOUNTED.equals(state)){
Log.d("myAppName", "Error: external storage is unavailable");
return 0;
}
if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
Log.d("myAppName", "Error: external storage is read only.");
return 0;
}
Log.d("myAppName", "External storage is not read only or unavailable");
if (ContextCompat.checkSelfPermission(this, // request permission when it is not granted.
Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
Log.d("myAppName", "permission:WRITE_EXTERNAL_STORAGE: NOT granted!");
// Should we show an explanation?
if (ActivityCompat.shouldShowRequestPermissionRationale(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// Show an expanation to the user *asynchronously* -- don't block
// this thread waiting for the user's response! After the user
// sees the explanation, try again to request the permission.
} else {
// No explanation needed, we can request the permission.
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant. The callback method gets the
// result of the request.
}
}
File folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM),folderName);
int result = 0;
if (folder.exists()) {
Log.d("myAppName","folder exist:"+folder.toString());
result = 2; // folder exist
}else{
try {
if (folder.mkdir()) {
Log.d("myAppName", "folder created:" + folder.toString());
result = 1; // folder created
} else {
Log.d("myAppName", "creat folder fails:" + folder.toString());
result = 0; // creat folder fails
}
}catch (Exception ecp){
ecp.printStackTrace();
}
}
return result;
}
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case MY_PERMISSIONS_REQUEST_WRITE_EXTERNAL_STORAGE: {
// If request is cancelled, the result arrays are empty.
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! Do the
// contacts-related task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'case' lines to check for other
// permissions this app might request
}
}
Run Code Online (Sandbox Code Playgroud)
更多信息请阅读" 在运行时请求权限 "
Mar*_*ues 16
SD卡的正确路径是
/mnt/sdcard/
Run Code Online (Sandbox Code Playgroud)
但是,如前所述,你不应该硬编码.如果您使用的是Android 2.1或之后,请使用
getExternalFilesDir(String type)
Run Code Online (Sandbox Code Playgroud)
除此以外:
Environment.getExternalStorageDirectory()
Run Code Online (Sandbox Code Playgroud)
仔细阅读https://developer.android.com/guide/topics/data/data-storage.html#filesExternal
此外,您还需要使用此方法或类似的方法
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;
}
Run Code Online (Sandbox Code Playgroud)
然后检查您是否可以访问SD卡.如上所述,阅读官方文档.
file.mkdirs()
Run Code Online (Sandbox Code Playgroud)
创建由此文件的尾随文件名命名的目录,包括创建此目录所需的完整目录路径.
使用mkdirs()而不是mkdir()..它对我有用:)
File folder = new File(Environment.getExternalStorageDirectory()+"/Saved CGPA");
if(!folder.exists()){
if(folder.mkdirs())
Toast.makeText(this, "New Folder Created", Toast.LENGTH_SHORT).show();
}
File sdCardFile = new File(Environment.getExternalStorageDirectory()+"/Saved CGPA/cgpa.html");
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
104774 次 |
| 最近记录: |