为什么我的根文件夹没有创建?
我尝试在我的主根目录中创建一个外部存储公共目录,并编写了以下代码行。代码编译正确,并且共享首选项已正确更新,但未在设备中创建根文件夹。
我使用 Atrix(它有内部存储,没有 SD 卡)。我还尝试下载 Gingerbread 的 Atrix Addon,并尝试在模拟器中编译它,但程序崩溃了。 (但它在真实设备中运行得很好。)
为什么模拟器崩溃(我的模拟器中有 1 GB SD 卡)? (它因空指针异常而崩溃)
为什么在真实设备中没有创建文件夹?
设备配置:
MinSDK - 8 SDK 版本 - 10
代码片段
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ixfer_main);
File rootDirectory = new File(getExternalFilesDir(null),"/AppRoot");
rootDirectory.mkdirs(); // also tried using mkdir() - still no good
编辑:这是权限问题。已修复并现在可以使用。
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I tried to create an external storage public directory in my main root directory and wrote the following lines of code. The code compiles properly, and shared preferences are updated correctly, but the root folder is not created in the device.
I use Atrix (it has this Internal Storage and no SD card). I also tried downloading the Atrix Addon for Gingerbread, and I tried compiling this in the emulator and the program crashes. (But it works perfectly well in real device.)
Why is the emulator crashing (I have 1 GB SD card in emulator)? (It crashes with a null pointer exception)
why is the folder not created in the real device?
Device Configurations:
MinSDK - 8
SDK version - 10
Code Snippet
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ixfer_main);
File rootDirectory = new File(getExternalFilesDir(null),"/AppRoot");
rootDirectory.mkdirs(); // also tried using mkdir() - still no good
EDIT: It was the permissions issue. Fixed and works now.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
完成之前的回答:
在对其执行任何操作之前,不要忘记检查外部存储状态。
to complete previous answer:
do not forget to check External Storage State before doing anything on it.
为了访问文件系统,您必须将适当的权限添加到您的 android 清单 xml 文件中。如果没有适当的权限,您的文件对象将不会被创建,并且您将获得空引用。
In order to access the file system, you must have the proper permission added to your android manifest xml file. Without the appropriate permissions, your file object won't be created and you'll get a null reference.