2016-03-11 53 views
2

,當我嘗試運行棉花糖這個代碼的文件夾沒有被創建.. 代碼,我試圖運行它的做工精細同樣的代碼,除了棉花糖不能創建在外部存儲文件夾中的棉花糖版本

File folder = new File(Environment.getExternalStorageDirectory() + "/abcdefg"); 
    boolean success = false; 
    if (!folder.exists()) { 
     success = folder.mkdir(); 
    } 
    if (!success) { 
     Log.d("", "Folder not created."); 
    } else { 
     Log.d("", "Folder created!"); 
    } 

回答

1

嘗試在您的活動中添加以下代碼以請求運行時權限。 您需要要求READ_EXTERNAL_STORAGE權限才能在外部存儲器中創建文件夾(目錄)。

if (ActivityCompat.checkSelfPermission(YourActivity.this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { 
      requestPermissions(new String[]{ Manifest.permission.WRITE_EXTERNAL_STORAGE}, 
        REQUEST_FOR_STORAGE);//REQUEST_FOR_STORAGE=1111 
     } else { 
//Do your stuff here 
      } 
... 

@Override 
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 

if(requestCode == REQUEST_FOR_STORAGE){ 
    //Do your stuff here 
    } 
} 

希望它能幫助你。

相關問題