2016-07-01 48 views
0

我在努力瞭解我的代碼正在發生什麼。這段代碼適用於EVERY(顯然)的手機,除了摩托羅拉(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被拋出。 AGAIN它適用於我測試過所有其他手機,所以大概是不是權限清單丟失

<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返回ExternalStorageDirectory,但mkdirs保持回國

  2. 嘗試變更getBestPath到總是返回內部路徑,然後返回mkdirs真正!在這個路徑:

    Internal path returned (注:Caminho:意味着Path:

    但我的朋友聲稱他無法打開文件,也沒有發現它與其他第三方文件瀏覽器。 當他試圖打開一個PDF,它說:「不可能查看PDF」和文件瀏覽器說:「超級用戶不可用」

  3. 沮喪,不知道發生了什麼,我向後退了一步恢復我的getBestPath方法按預期運行,改變了呼籲folder.mkdirs()Files.createParentDirs(folder)希望一些更具體的錯誤,而是我剛剛接到一個簡單:

    createParentDirs result

嘛,我爲什麼不能創建文件夾/文件並打開it JUST IN MOTOROLA PHONES?這裏有人已經遇到過這個問題我做錯了什麼線索?

+1

「但我的朋友聲稱,他無法打開文件,也沒有與其他第三方文件瀏覽器找到它」 - 這是多麼[內部存儲](https://commonsware.com/blog/2014/04/07/storage-situation-internal-storage.html)的作品。 「當他試圖打開PDF時,它會說:」無法查看PDF「」 - 第三方應用程序沒有直接訪問您應用程序內部存儲的文件系統。 「我做錯了什麼線索?」 - 您正在粘貼對話框的圖像,而不是您遇到的異常的Java堆棧跟蹤。 – CommonsWare

+1

此外,也許你的失敗設備碰巧是你正在測試的唯一的Android 6.0設備,在這種情況下,你應該看看運行時權限:https://stackoverflow.com/questions/32635704/android-permission-doesnt-work-即使如果我有宣佈它 – CommonsWare

+0

@CommonsWare,我不能讓他電話調試本地,對不起:/因此,'mkdirs'不會引發異常,它只是返回false。 – Latrova

回答

0

正如@CommonsWare所指出的那樣,這不是摩托羅拉問題,而是Android 6.0版本。 我剛剛添加了支持請求權限(運行時),並且還閱讀article建議(偉大的文章,真的值得)。

我只是說我的活動如下:

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(); 
     } 
    } 
} 
相關問題