Lizard Lizard - 1 year ago 85
Android Question

Can't create folders in MOTOROLA(Android 6.0) phones

I'm struggling to find out what's going on with my code. This code snippet works on EVERY (apparently) phone, except motorola (Moto G, etc...):

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

throw new CantCreateFolderPathException(folderPath);

And I generate the folder path with this:

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;

keeps returning false, which causes
to be thrown. AGAIN, it works on all other phones that I've tested, so probably it's not a permission manifest missing:

<manifest xmlns: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"/>


I asked for help from a friend of mine, since I don't have a motorola. I created a APK with some dialogs to help me understand the error, my steps were:

  1. Find out which path is the "best one" generated from my code: getBestPath returns ExternalStorageDirectory, but mkdirs keeps returning false

  2. Tried to change getBestPath to always returns internal path, then mkdirs returned true! At this path:

    Internal path returned

    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. Frustrated with no clue of what's happening, I took a step back reverting my getBestPath method to work as expected and changed the call for
    hoping for some more descriptive error, but instead I just received a simple:

    createParentDirs result

Well, why I can't create folder/file and open it JUST IN MOTOROLA PHONES? Anyone here already faced this? Any clues of what I'm doing wrong?

Answer Source

As @CommonsWare pointed, it's not a MOTOROLA issue, but an Android 6.0 one. I just added support to ask for permissions (runtime) and also read the article suggested (Great article, really worth it).

I just added the following in my activity:

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

private void explainOrAskPermission() {
    if (ActivityCompat.shouldShowRequestPermissionRationale(this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
        new AlertDialog.Builder(this)
            .setNeutralButton(R.string.ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialogInterface, int i) {
    else {

private void askPermission() {
            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},

public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
        else {
            new AlertDialog.Builder(this)
                .setNeutralButton(R.string.ok, null)