简体   繁体   English

无法在 Android 上的 SD 卡中创建文件夹

[英]Cannot create folder in SD card on Android

I have this problem in my application.我在我的应用程序中遇到了这个问题。 I want to create a folder in Emulator SD card but it didn't work and I don't know why.我想在模拟器 SD 卡中创建一个文件夹,但它不起作用,我不知道为什么。 I tried mkdir and mkdirs.我尝试了 mkdir 和 mkdirs。

I added WRITE_EXTERNAL_STORAGE Permission to the Manifest.我向清单添加了 WRITE_EXTERNAL_STORAGE 权限。 I tried with MediaScannerConnection and didn't work.我尝试使用 MediaScannerConnection 但没有用。 I've searched a lot the web and tried a lot of implementations but no change.我在网上搜索了很多并尝试了很多实现但没有改变。 What could be the issue?可能是什么问题?

Here is my code:这是我的代码:

    public void createDirIfNotExists() {


    File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/cdp");
    boolean success = true;
    if (!folder.exists()) {
        success = folder.mkdirs();
    }
    if (success) {

        Log.e("Folder: ", "folder created!!");
    } else {
        Log.e("Folder Error: ", "folder cannot be created!!");
    }
}

My manifest look like this:我的清单看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.aboussof.myapplication" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WAKE_LOCK" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
    </activity>

    <activity android:name=".Test">

        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>

    </activity>
</application>

</manifest>

And so I try to create the folder:所以我尝试创建文件夹:

    @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);
    mProgressDialog = new ProgressDialog(Test.this);
    mProgressDialog.setMessage("A message");
    mProgressDialog.setIndeterminate(true);
    mProgressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
    mProgressDialog.setCancelable(true);

    // execute this when the downloader must be fired
    final DownloadTask downloadTask = new DownloadTask(Test.this);
    createDirIfNotExists();
    downloadTask.execute("http://www.sample-videos.com/video/mp4/720/big_buck_bunny_720p_5mb.mp4");

    mProgressDialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            downloadTask.cancel(true);
        }
    });

}

Replace代替

File folder = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/cdp");

with

File folder = new File(Environment.getExternalStorageDirectory() + "/", "cdp");

Have you solved your issue yet?你解决你的问题了吗? I use your method with a tiny change at我用你的方法做了一个微小的改变

File folder = new File(Environment.getExternalStorageDirectory(), "cdp");

And the result like the following screenshot:结果如下图所示:

在此处输入图像描述

在此处输入图像描述

My AVD configuration like the following:我的 AVD 配置如下:

在此处输入图像描述

I have had the same issue - file.mkdirs() returns false on android emulator API 23. And all configs was set according to BNK's answer.我遇到了同样的问题 - file.mkdirs() 在 android 模拟器 API 23 上返回 false。所有配置都是根据 BNK 的回答设置的。 Try to change API to 19 - it works for me.尝试将 API 更改为 19 - 它对我有用。 Looks like weird emulator/android version issue.看起来像奇怪的模拟器/android 版本问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM