2016-06-30 44 views
1

我正嘗試使用圖像文件創建文件夾。無法在Android中創建具有圖像的文件夾

這裏是代碼

File folder = new File(Environment.getExternalStorageDirectory().toString()+"/Aqeel/Images"); 
    folder.mkdirs(); 
    String extStorageDirectory = folder.toString(); 
    File file = new File(extStorageDirectory, "Image2.PNG"); 
    if(!folder.exists()){ 
     Toast.makeText(MainActivity.this, "Not Exist", Toast.LENGTH_SHORT).show(); 
    } 

這裏是我的清單過於

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.bernardo.hellow"> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> 
<application 
    android:allowBackup="true" 
    android:icon="@mipmap/ic_launcher" 
    android:label="@string/app_name" 
    android:supportsRtl="true" 
    android:theme="@style/AppTheme"> 
    <activity 
     android:name=".hello" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme.NoActionBar"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

</manifest> 

我已經添加了WRITE_EXTERNAL_STORAGE權限,但它仍然沒有工作。

+2

什麼是 「不工作」 是什麼意思?你認爲它應該做什麼,它做什麼呢? –

+0

@Bernardo D'Agostino你的targetSdk? – Nisarg

+0

您必須得到SecurityException,請檢查! –

回答

3

你,如果你的目標API上運行23

請求允許

// Here, thisActivity is the current activity 
if (ContextCompat.checkSelfPermission(thisActivity, 
      Manifest.permission.READ_CONTACTS) 
    != PackageManager.PERMISSION_GRANTED) { 

// Should we show an explanation? 
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity, 
     Manifest.permission.READ_CONTACTS)) { 

    // Show an expanation to the user *asynchronously* -- don't block 
    // this thread waiting for the user's response! After the user 
    // sees the explanation, try again to request the permission. 

} else { 

    // No explanation needed, we can request the permission. 

    ActivityCompat.requestPermissions(thisActivity, 
      new String[]{Manifest.permission.READ_CONTACTS}, 
      MY_PERMISSIONS_REQUEST_READ_CONTACTS); 

    // MY_PERMISSIONS_REQUEST_READ_CONTACTS is an 
    // app-defined int constant. The callback method gets the 
    // result of the request. 
    } 
} 

來源,請求許可程序:Android開發者

顯然,你需要改變READ_CONTACTS寫入WRITE_EXTERNAL_STORAGE

處理結果

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

    switch (requestCode) { 

    case MY_PERMISSIONS_REQUEST_READ_CONTACTS: { 
     // If request is cancelled, the result arrays are empty. 
     if (grantResults.length > 0 
      && grantResults[0] == PackageManager.PERMISSION_GRANTED) { 

      // permission was granted, yay! Do the 
      // contacts-related task you need to do. 

     } else { 

      // permission denied, boo! Disable the 
      // functionality that depends on this permission. 
     } 
     return; 
    } 

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

來源:Android開發者

+0

我不確定OP說他瞄準的目標是23+,無論如何我認爲有必要提一下,清單屬性的權限是不同的<23 and > = 23 https://developer.android.com/guide/topics/manifest/uses-permission-sdk-23-element.html –

+0

是的,我同意,但我在我的答案開始時做了*如果你的目標是api 23 *。但你是對的:) –

相關問題