2016-06-01 89 views
0

我實際上能夠捕捉照片並將其保存在android外部存儲DCIM文件夾中。如何在android中創建一個新文件夾DCIM

我的問題是,我不能在裏面創建一個新的文件夾,所以DCIM/MyPic.jpg成爲DCIM/MyFolder中/ MyPic.jpg

這裏是我的源代碼:

File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "address"); 

if (!f.exists()) { 
    f.mkdir(); 
} 

File file = new File(Environment.getExternalStorageDirectory() 
    + File.separator 
    + "DCIM" 
    + File.separator 
    + "address" 
    + File.separator 
    , "IMG_001.jpg"); 

請注意,我正確的問WRITE_EXTERNAL_STORAGE許可清單中。

拍攝照片的意圖部分很好,因爲我可以直接將它保存到DCIM。

我沒有得到任何錯誤消息,但沒有任何反應......創造了無「地址」的文件夾:(

感謝您的幫助:d

+0

您正在運行哪個目標平臺? –

+0

Min:15,Target:23.我可以提到f.mkdir()總是返回false。 –

+1

我閱讀這篇文章,但它不幫助我解決我的問題:http://stackoverflow.com/questions/7212559/android-mkdir-returns-false –

回答

2

張貼在評論,我想你的代碼,並它爲我工作。

MainActivity.java

public class MainActivity extends Activity { 

    private final static String TAG = MainActivity.class.getSimpleName(); 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     final File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "address"); 

     if (!f.exists()) { 
      Log.d(TAG, "Folder doesn't exist, creating it..."); 
      boolean rv = f.mkdir(); 
      Log.d(TAG, "Folder creation " + (rv ? "success" : "failed")); 
     } else { 
      Log.d(TAG, "Folder already exists."); 
     } 
    } 
} 

AndroidManifest。 XML

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.tristan.testcreatedirectory"> 

<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=".MainActivity"> 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

</application> 

啓動應用程序我必須手動啓用權限,因爲我是在Android 6.

**日誌啓動應用程序時首次**之前

D/MainActivity: Folder doesn't exist, creating it... 
D/MainActivity: Folder creation success 

**第二次啓動應用程序時的日誌**

D/MainActivity: Folder already exists. 
+0

謝謝很多爲你的答案,但相同的代碼不適用於我:(我無法創建目錄,然後保存在其中的圖像。 –

+0

當您運行我的代碼時是什麼日誌?可以將它粘貼給我嗎? – Tristan

+0

D/MainActivity:文件夾不存在,創建它... D/MainActivity:文件夾創建失敗 –

4
boolean hasPermission = (ContextCompat.checkSelfPermission(this, 
     Manifest.permission.WRITE_EXTERNAL_STORAGE) == PackageManager.PERMISSION_GRANTED); 

if (!hasPermission) { 
    ActivityCompat.requestPermissions(this, 
      new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 
      REQUEST_WRITE_STORAGE); 
} 

else { 
    createFolder(); 
} 

@Override 
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) { 
    super.onRequestPermissionsResult(requestCode, permissions, grantResults); 
    switch (requestCode) 
    { 
     case REQUEST_WRITE_STORAGE: { 
      if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) 
      { 
       //reload my activity with permission granted or use the features what required the permission 
       createFolder(); 

      } else 
      { 
       Toast.makeText(this, "The app was not allowed to write to your storage. Hence, it cannot function properly. Please consider granting it this permission", Toast.LENGTH_LONG).show(); 
      } 
     } 
    } 

} 

public void createFolder() { 

    final File f = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM), "address"); 

    if (!f.exists()) { 
     Toast.makeText(this, "Folder doesn't exist, creating it...", Toast.LENGTH_SHORT).show(); 
     boolean rv = f.mkdir(); 
     Toast.makeText(this, "Folder creation " + (rv ? "success" : "failed"), Toast.LENGTH_SHORT).show(); 
    } else { 
     Toast.makeText(this, "Folder already exists.", Toast.LENGTH_SHORT).show(); 
    } 

} 
相關問題