2017-03-16 62 views
0

您好所有,Android的:不能在SD卡寫入三星設備與棉花糖

我想在三星設備的SD卡與棉花糖創建一個文件夾。但是,api,dir.canWrite()失敗了,這意味着我不能寫入該路徑。同樣我在moto x中嘗試過它的工作。

有人請幫助我

更新:

我在清單的所有權限,也爲運行許可的代碼。

清單文件:

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

運行許可代碼:

private final int WRITE_STORAGE_CODE = 10; 
@TargetApi(23) 
public boolean isStoragePermissionGrantedForWriteStorage() { 
    if (Build.VERSION.SDK_INT >= 23) { 
     if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE) 
       == PackageManager.PERMISSION_GRANTED) { 
      Log.v(TAG,"Permission is granted"); 
      return true; 
     } else { 

      Log.v(TAG,"Permission is revoked"); 
      ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, WRITE_STORAGE_CODE); 
      return false; 
     } 
    } 
    else { //permission is automatically granted on sdk<23 upon installation 
     Log.v(TAG,"Permission is granted"); 
     return true; 
    } 
} 

@Override 
public void onRequestPermissionsResult(int requestCode, 
     String permissions[], int[] grantResults) { 
    switch (requestCode) { 
     case WRITE_STORAGE_CODE: { 
      // If request is cancelled, the result arrays are empty. 
      if (grantResults.length > 0 
       && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

       File dir = new File("/storage/6A6B-1CEF/"); // This is the memory card location. 

       if(dir.canWrite()) 
       { 
        Log.d("Activity", can write here!!); 
       }else{ 
        Log.d("Activity", oops!!!!, can't write here!!);     } 
      } else { 

       // permission denied, boo! Disable the 
       // functionality that depends on this permission. 
       Log.d("Activity", permission denied); 
      } 
      return; 
     } 

     // other 'case' lines to check for other 
     // permissions this app might request 
    } 
} 

所以,每當我在三星設備調用此代碼,它的打印,我不能在該位置寫。但是相同的代碼允許我在Moto x設備中編寫代碼。

注:

我已閱讀,棉花糖必須有存儲卡,無論是「內部存儲」或「移動存儲」選項。我選擇了兩款手機(三星和摩托羅拉)的便攜式存儲器,但該代碼僅在三星中出現故障。

+1

是否增加了運行時權限? – rafsanahmad007

+1

在Marshmallow中,你需要在做任何文件操作之前先要求權限,否則會給出SecurityException。 –

+0

'但是API,dir.canWrite()失敗'。不要這樣想。如果無法寫入,該函數將返回false。 – greenapps

回答

0

您在清單文件中添加了權限?

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> 
+0

是的,我有..我也更新了這個問題。 @ M.Saad Lakhan –