无法在某些特定设备中创建文件夹

Can't create folders in some specific devices

我正在努力找出我的代码发生了什么。此代码片段适用于 EVERY(显然)phone,摩托罗拉(Moto G 等)除外:

private void createFolder(String folderPath) throws CantCreateFolderPathException {
    File folder = new File(folderPath);
    if (folder.mkdirs() || folder.isDirectory())
      return; //Everything ok! 

    throw new CantCreateFolderPathException(folderPath);
}

我用这个生成文件夹路径:

public String getFolderPath(Courseware courseware) {
    String path = getBestPath() + courseware.getSubject().getName() + File.separator;
    UnisantaApplication.Log_i("SAVE PATH:" + path);
    return path;
    //Result example: sd/unisantaapp/material/SUBJECT NAME/
}

private String getBestPath() {
    if (isExternalStorageWritable())
        return getExternalPath();
    return getInternalPath();
}

private String getExternalPath() {
    return Environment.getExternalStorageDirectory()
            .getAbsolutePath() + File.separator +
            "unisantaapp" + File.separator +
            "material" + File.separator;
}

private String getInternalPath() {
    return UnisantaApplication.getInstance().getApplicationContext()
            .getFilesDir().getAbsolutePath() + File.separator +
            "material" + File.separator;
}

folder.mkdirs 一直返回 false,这导致 CantCreateFolderPathException 被抛出。 再次它适用于我测试过的所有其他 phone,所以可能 不是许可清单缺失

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="si.unisanta.tcc.unisantaapp" >

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

<application
      [...]

因为我没有摩托罗拉,所以我向朋友求助。我创建了一个带有一些对话框的 APK 来帮助我理解错误,我的步骤是:

  1. 从我的代码getBestPath returns ExternalStorageDirectory找出"best one"是哪个路径,但是mkdirs一直返回

  2. 试图将 getBestPath 更改为 始终 returns 内部路径 ,然后 mkdirs 返回 true!在这个路径:

    (Note: Caminho: means Path:)

    But my friend claims that he can't open the file nor find it with another third-party file explorer. When he tries to open a PDF, it says:"Impossible to view PDF" and the file explorer says: "superuser not available".

  3. 因为不知道发生了什么事而感到沮丧,我退后一步,恢复我的 getBestPath 方法以按预期工作,并将对 folder.mkdirs() 的调用更改为 Files.createParentDirs(folder) 希望一些更具描述性的错误,但我只是收到了一个简单的:

嗯,为什么我不能创建 folder/file 并打开它 仅在摩托罗拉手机中?这里有人已经遇到过这个吗?关于我做错了什么的任何线索?

正如@CommonsWare 指出的那样,这不是摩托罗拉的问题,而是 Android 6.0 的问题。 我刚刚添加了请求权限(运行时)的支持,还阅读了 article 建议(很棒的文章,真的很值得)。

我刚刚在 activity 中添加了以下内容:

private boolean hasPermissionToDownload() {
    int permissionResult = ContextCompat.checkSelfPermission(this,
            Manifest.permission.WRITE_EXTERNAL_STORAGE);
    return permissionResult == PackageManager.PERMISSION_GRANTED;
}

private void explainOrAskPermission() {
    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        new AlertDialog.Builder(this)
            .setTitle(R.string.download_permission_explain_title)
            .setMessage(getString(R.string.download_permission_explain_message))
            .setNeutralButton(R.string.ok, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    askPermission();
                }
            })
            .show();
    }
    else {
        askPermission();
    }
}

private void askPermission() {
    ActivityCompat.requestPermissions(this,
            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
            PERMISSION_SAVE_DOWNLOAD_REQUEST);
}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
    if (requestCode == PERMISSION_SAVE_DOWNLOAD_REQUEST) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
            downloadCourseware(lastCoursewareClicked);
        }
        else {
            new AlertDialog.Builder(this)
                .setTitle(R.string.download_permission_denied_title)
                .setMessage(R.string.download_permission_denied_message)
                .setNeutralButton(R.string.ok, null)
                .show();
        }
    }
}