我想将加速度计记录的数据存储在外部存储器上,但当我尝试创建要保存数据的目录时出现错误。我的代码如下:
dataDirectory=Environment.getDataDirectory();
path=DataDirectory.getAbsolutePath();
path+="/"+getResources().getString(R.string.DirectoryName);
myDirectory=new File(path);
myDirectory.createNewFile();
createNewFile
调用触发IOException
。
我可以添加两件事:在调用 getAbsolutePath
后将 path
变量设置为“/data”,并且 getExternalStorageState
函数返回“mounted” 。
谁能告诉我我的代码有什么问题吗?
预先感谢您花时间帮助我。
I want to store data recorded with the accelerometer on the external storage but I have an error when I try to create the directory in which I want to save the data. My code is as follows :
dataDirectory=Environment.getDataDirectory();
path=DataDirectory.getAbsolutePath();
path+="/"+getResources().getString(R.string.DirectoryName);
myDirectory=new File(path);
myDirectory.createNewFile();
The createNewFile
call triggers an IOException
.
I can add two things : the path
variable is set to "/data" after the call to getAbsolutePath
and the getExternalStorageState
function returns "mounted".
Can anyone tell me what is wrong in my code?
Thanks in advance for the time you will spend trying to help me.
发布评论
评论(2)
您没有权限在
/data
目录中写入/data/data/com.yourapplication/files
目录中工作有许可。使用 Context.getFilesDir() 获取应用程序工作目录的路径。You can't have permission to write in the
/data
directory/data/data/com.yourapplication/files
directory where you do have permission. UseContext.getFilesDir()
to get the path of your application's working directory.尝试
在清单文件中添加
WRITE_EXTERNAL_STORAGE
权限try this
add
WRITE_EXTERNAL_STORAGE
permission in your manifest file