如何在android DCIM中创建一个新文件夹

时间:2016-06-01 00:37:57

标签: android

我实际上能够捕获照片并将其保存在android外部存储DCIM文件夹中。

我的问题是我无法在其中创建新文件夹,因此 DCIM / MyPic.jpg 变为 DCIM / MyFolder / MyPic.jpg 。< / p>

这是我的源代码:

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。

我没有收到任何错误消息,但没有任何反应......没有&#39;地址&#39;文件夹创建:(

感谢您的帮助:D

2 个答案:

答案 0 :(得分: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();
    }

}

答案 1 :(得分:3)

正如评论中所述,我尝试了您的代码,它对我有用。

<强> 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.