简体   繁体   English

无法在SD卡上创建应用程序目录

[英]Can't create application directory on SD card

I'm developing an app that should store photos on SD card. 我正在开发一个应将照片存储在SD卡上的应用。 First the SD card path is retrieved in the code, since getExternalStorageDirectory() doesn't return the accurate SD card path. 首先,由于getExternalStorageDirectory()没有返回准确的SD卡路径,因此在代码中检索了SD卡路径。 Then I'm trying to create a directory to store the pictures: 然后,我试图创建一个目录来存储图片:

String PACKAGE_NAME = "com.example.me.sdapp";
// sdpath = "/storage/extSdCard/"
String path = sdpath+"Android/data/"+PACKAGE_NAME;
File folder = new File(path);
if (!folder.exists()){
   var = folder.mkdirs();
   if (!folder.canWrite()){
       Log.d("SD Card app", "Directory is not writable");
   }
}

The directory is not built and the log message does appear. 未构建目录,并且确实出现日志消息。 Hence I guess I can't write to that space. 因此,我想我不能写那个空间。 However, I can create manually the directory when I run with adb shell: 但是,当我使用adb shell运行时,可以手动创建目录:

mkdir /storage/extSdCard/Android/data/com.example.me.sdapp

And after creating the directory manually, the pictures can be stored in it from the app. 手动创建目录后,可以从应用程序将图片存储在其中。

Why can't I create my application folder on the SD card from the app, and yet once the folder has been created manually, I can still write files to it from the app? 为什么不能通过应用程序在SD卡上创建我的应用程序文件夹,但是一旦手动创建了文件夹,我仍然可以通过应用程序向其中写入文件? And how could I achieve creating that folder from the application? 我如何从应用程序创建该文件夹?

I have tried to make it work with and without the permission in the manifest: 我试图使它在清单中有无许可的情况下都可以工作:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE>

But I guess that wouldn't be the problem anyway since as soon as I manually create the directory, the app is able to write in it. 但是我想这绝对不是问题,因为一旦我手动创建目录,应用程序就可以在其中写入内容。

The device is a Samsung Galaxy Tab III and Android version is 4.4.2. 该设备是Samsung Galaxy Tab III,Android版本是4.4.2。

EDIT 编辑

As some people marked my question as duplicate of this one , I will try to explain why that other post doesn't address my issue. 当有人将我的问题标记为与问题重复时,我将尝试解释为什么其他帖子没有解决我的问题。

  • All the answers refer to the method getExternalStorageDirectory() which does not always point to SD card; 所有答案都指向方法getExternalStorageDirectory() ,该方法并不总是指向SD卡;它总是指向SD卡。 in my case, it points to internal storage. 就我而言,它指向内部存储。 Hence when I use this method, I can indeed create my app folder from the application, but the folder will be created in internal storage and not on SD card. 因此,当我使用这种方法时,确实可以从应用程序创建应用程序文件夹,但是该文件夹将在内部存储中创建,而不是在SD卡上。

  • Some answers mention the manifest permission, which I said I have taken into account already. 一些答案提到清单许可,我说我已经考虑过了。

**For Path retrieving ** **用于路径检索**

File direct = new File(Environment.getExternalStorageDirectory() + "Your folder path" );
       if (!direct.exists()) {
                              mkdir();
                                         }

This will give you correct path of your sdcard. 这将为您提供正确的SD卡路径。

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

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