Android mkdir 未创建文件夹

发布于 2024-11-06 07:55:12 字数 1276 浏览 2 评论 0原文

今晚我在做一些我认为很简单的事情时遇到了问题......在 /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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

梦萦几度 2024-11-13 07:55:12

/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.

夏了南城 2024-11-13 07:55:12

检查您将权限放置在何处,它们必须按以下方式放置:

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="edu.una.info.app">
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
   <uses-permission android:name="android.permission.CALL_PHONE" />

<application android:allowBackup="true"
    android:label="@string/app_name"
    android:icon="@drawable/ic_launcher"
    android:theme="@style/AppTheme">

    <activity
        android:name="edu.una.info.SplashScreenActivity"

Check where you are putting the permissions they must go in this way:

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
 package="edu.una.info.app">
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
   <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
   <uses-permission android:name="android.permission.CALL_PHONE" />

<application android:allowBackup="true"
    android:label="@string/app_name"
    android:icon="@drawable/ic_launcher"
    android:theme="@style/AppTheme">

    <activity
        android:name="edu.una.info.SplashScreenActivity"
梦在深巷 2024-11-13 07:55:12

尝试使用不同的设备。我的模拟器设备存在问题,但当我连接 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文