简体   繁体   English

安卓:IOException:权限被拒绝

[英]Android: IOException: Permission denied

I'm creating/saving file on SD card.我正在 SD 卡上创建/保存文件。 This is the code:这是代码:

File sdDir = Environment.getExternalStorageDirectory();
public void btnsave_clicked(View v) throws FileNotFoundException, IOException{
     File f;
      f=new File(sdDir, "deposit.dma");
      if(!f.exists())
      f.createNewFile();
...
}

But when I click the button, it throws "Permission denied".但是当我点击按钮时,它会抛出“权限被拒绝”。 Don't ask me for add uses-permission.不要向我询问添加使用权限。

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

I've already added this in Manifest.我已经在清单中添加了这个。

What should I do?我应该怎么办? LogCat info:日志猫信息:

10-19 20:25:14.681: E/AndroidRuntime(29016): FATAL EXCEPTION: main
10-19 20:25:14.681: E/AndroidRuntime(29016): java.lang.IllegalStateException: Could not execute method of the activity
10-19 20:25:14.681: E/AndroidRuntime(29016):    at android.view.View$1.onClick(View.java:2168)
10-19 20:25:14.681: E/AndroidRuntime(29016):    at android.view.View.performClick(View.java:2552)
10-19 20:25:14.681: E/AndroidRuntime(29016):    at android.view.View$PerformClick.run(View.java:9229)
10-19 20:25:14.681: E/AndroidRuntime(29016):    at android.os.Handler.handleCallback(Handler.java:587)
10-19 20:25:14.681: E/AndroidRuntime(29016):    at android.os.Handler.dispatchMessage(Handler.java:92)
10-19 20:25:14.681: E/AndroidRuntime(29016):    at android.os.Looper.loop(Looper.java:130)
10-19 20:25:14.681: E/AndroidRuntime(29016):    at android.app.ActivityThread.main(ActivityThread.java:3701)
10-19 20:25:14.681: E/AndroidRuntime(29016):    at java.lang.reflect.Method.invokeNative(Native Method)
10-19 20:25:14.681: E/AndroidRuntime(29016):    at java.lang.reflect.Method.invoke(Method.java:507)
10-19 20:25:14.681: E/AndroidRuntime(29016):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
10-19 20:25:14.681: E/AndroidRuntime(29016):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
10-19 20:25:14.681: E/AndroidRuntime(29016):    at dalvik.system.NativeStart.main(Native Method)
10-19 20:25:14.681: E/AndroidRuntime(29016): Caused by: java.lang.reflect.InvocationTargetException
10-19 20:25:14.681: E/AndroidRuntime(29016):    at java.lang.reflect.Method.invokeNative(Native Method)
10-19 20:25:14.681: E/AndroidRuntime(29016):    at java.lang.reflect.Method.invoke(Method.java:507)
10-19 20:25:14.681: E/AndroidRuntime(29016):    at android.view.View$1.onClick(View.java:2163)
10-19 20:25:14.681: E/AndroidRuntime(29016):    ... 11 more
10-19 20:25:14.681: E/AndroidRuntime(29016): Caused by: java.io.IOException: Permission denied
10-19 20:25:14.681: E/AndroidRuntime(29016):    at java.io.File.createNewFileImpl(Native Method)
10-19 20:25:14.681: E/AndroidRuntime(29016):    at java.io.File.createNewFile(File.java:1257)
10-19 20:25:14.681: E/AndroidRuntime(29016):    at org.brotheroftux.depositmaster.MainActivity.btnsave_clicked(MainActivity.java:43)
10-19 20:25:14.681: E/AndroidRuntime(29016):    ... 14 more

From the Android Environment doc来自Android 环境文档

Applications should not directly use this top-level directory, in order to avoid polluting the user's root namespace.应用程序不应该直接使用这个顶级目录,以免污染用户的根命名空间。 Any files that are private to the application should be placed in a directory returned by Context.getExternalFilesDir, which the system will take care of deleting if the application is uninstalled.应用程序私有的任何文件都应放在 Context.getExternalFilesDir 返回的目录中,如果应用程序被卸载,系统将负责删除该目录。 Other shared files should be placed in one of the directories returned by getExternalStoragePublicDirectory(String).其他共享文件应放在 getExternalStoragePublicDirectory(String) 返回的目录之一中。

In other words, this is not really a good practice (although perhaps you have some very good reason; the context isn't very clear).换句话说,这并不是一个好的做法(尽管您可能有一些很好的理由;上下文不是很清楚)。

Additionally, as Cthulhu and Ted Hopp commented, make sure the permission is in the correct location in the manifest.此外,正如 Cthulhu 和 Ted Hopp 评论的那样,请确保权限位于清单中的正确位置。

Some SD and MicroSD cards have write-protect switches on them.一些 SD 和 MicroSD 卡上有写保护开关。 If yours has one, make sure that it is not write-protected.如果您有,请确保它没有写保护。

Also, after you plug your phone in, make sure you have not selected "Turn on USB storage."另外,插入手机后,请确保您没有选择“打开 USB 存储”。 This will mount your SD card for access by your PC and disable writing by your device.这将安装您的 SD 卡以供您的 PC 访问并禁用您的设备写入。

You can check this by swiping the notification bar down from the top.您可以通过从顶部向下滑动通知栏来检查这一点。 Mine says "USB Connected" which is ok, but "Turn on USB storage" should not be enabled.我的显示“USB 连接”没问题,但不应启用“打开 USB 存储”。

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

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