简体   繁体   English

如何在Android DCIM中创建新文件夹

[英]How to create a new folder in android DCIM

I am actually able to capture a photo and to save it in android external storage DCIM folder. 我实际上能够捕获照片并将其保存在android外部存储DCIM文件夹中。

My problem is that I can't create a new folder in it, so DCIM/MyPic.jpg became DCIM/MyFolder/MyPic.jpg . 我的问题是我无法在其中创建新文件夹,所以DCIM / MyPic.jpg变成了DCIM / MyFolder / MyPic.jpg

Here is my source code : 这是我的源代码:

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");

Notice that I correctly asked for WRITE_EXTERNAL_STORAGE permission in manifest. 请注意,我正确要求了清单中的WRITE_EXTERNAL_STORAGE权限。

The intent part to capture photo is fine, because I can save it directly to DCIM. 捕获照片的意图部分很好,因为我可以将其直接保存到DCIM。

I do not get any error message, but nothing happens... no 'address' folder created :( 我没有收到任何错误消息,但是什么也没有发生...没有创建'address'文件夹:(

Thanks for help :D 感谢您的帮助:D

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();
    }

}

As posted in the comments, I tried your code and it worked for me. 如评论中所述,我尝试了您的代码,并且对我有用。

MainActivity.java 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 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>

Before launching the app I have to enable the permission manually because I am on Android 6. 在启动应用程序之前,我必须手动启用权限,因为我使用的是Android 6。

** Logs when launching the app the first time ** **首次启动应用程序时记录**

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

** Logs when launching the app the second time ** **第二次启动应用程序时记录**

D/MainActivity: Folder already exists.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM