Android mkdir 未创建文件夹
今晚我在做一些我认为很简单的事情时遇到了问题......在 /mnt/sdcard 中创建一个文件夹。
我已设置以下权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
我的 Main.java
具有以下内容来创建文件夹:
public class Main extends TabActivity {
static int index = 1;
private static final String TAG = "Main";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
File folder = new File(Environment.getExternalStorageDirectory () + "/tallgrass/images");
boolean success = false;
if(!folder.exists()){
success = folder.mkdir();
}
if (!success){
Log.d(TAG,"Folder not created.");
}
else{
Log.d(TAG,"Folder created!");
}
}
我收到“文件夹已创建!”消息在我的日志中,但当我检查 /mnt/sdcard
和 /sdcard
时,两者都没有该文件夹。我尝试过调用:
Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
它返回true。我就是想不通这个问题,因为所有迹象都表明它应该有效。我还尝试过将手机与电脑断开连接,以防安装 SD 卡或其他原因,因为我目前正在使用手机而不是模拟器进行开发。说到这里,将 debuggable
设置为 true
是否可能会阻止它创建文件夹?
谢谢!
Tonight I am currently having issues doing something that I thought would be simple... making a folder in /mnt/sdcard.
I have set the follow permission:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
My Main.java
has the following to make the folder:
public class Main extends TabActivity {
static int index = 1;
private static final String TAG = "Main";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
File folder = new File(Environment.getExternalStorageDirectory () + "/tallgrass/images");
boolean success = false;
if(!folder.exists()){
success = folder.mkdir();
}
if (!success){
Log.d(TAG,"Folder not created.");
}
else{
Log.d(TAG,"Folder created!");
}
}
I get the "Folder created!" message in my log but when I check both /mnt/sdcard
and /sdcard
neither one has the folder. I have tried calling:
Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())
and it returns true. I just can't figure this one out because all signs are pointing that it should work. I have also tried it with the phone disconnected from the PC in case the SD card was mounting or something as I am currently using my phone instead of the emulator for developing. Speaking of which, does debuggable
to true
maybe prevent it from making the folder?
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
/mnt/sdcard/tallgrass/
目录是否存在? (我猜不是,但你永远不知道。)文件.mkdirs()
方法将创建所有需要的目录;mkdir()
只会在路径名中创建last 目录。Does the
/mnt/sdcard/tallgrass/
directory exist? (I'm guessing not, but you never know.)The
File.mkdirs()
method will create all needed directories;mkdir()
will only create the last directory in the pathname.检查您将权限放置在何处,它们必须按以下方式放置:
Check where you are putting the permissions they must go in this way:
尝试使用不同的设备。我的模拟器设备存在问题,但当我连接 Oneplus 5T 时,它创建了一个文件夹(我的 Oneplus 没有外部 MicroSD)。
但与此同时,我的模拟器(Pixel 3)根本不创建文件夹。
至少权限都是好的。
Try Using Different Device. The problem exists with my Emulator Device, but when I connected my Oneplus 5T, it created a Folder (My Oneplus Does not have a External MicroSD).
But at the same time, my emulator (Pixel 3) does not create a Folder at all.
At least the Permissions are all good.