安卓:IOException异常:权限被拒绝 [英] Android: IOException: Permission denied

查看:228
本文介绍了安卓:IOException异常:权限被拒绝的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建/保存在SD卡上的文件。这是code:

 文件sdDir = Environment.getExternalStorageDirectory();
btnsave_clicked(视图V)公共无效抛出FileNotFoundException异常,IOException异常{
     文件f;
      F =新的文件(sdDirdeposit.dma);
      如果(!f.exists())
      f.createNewFile();
...
}

但是,当我按一下按钮,它会抛出权限被拒绝。
不要问我要添加使用许可权。

 <使用许可权的android:NAME =android.permission.WRITE_EXTERNAL_STORAG​​E/>

我已经添加了此清单中。

我应该怎么办?
LogCat中的信息:

  20 10-19:25:14.681:E / AndroidRuntime(29016):致命异常:主要
10-19 20:25:14.681:E / AndroidRuntime(29016):java.lang.IllegalStateException:无法执行活动的方法
10-19 20:25:14.681:E / AndroidRuntime(29016):在android.view.View $ 1.onClick(View.java:2168)
10-19 20:25:14.681:E / AndroidRuntime(29016):在android.view.View.performClick(View.java:2552)
10-19 20:25:14.681:E / AndroidRuntime(29016):在android.view.View $ PerformClick.run(View.java:9229)
10-19 20:25:14.681:E / AndroidRuntime(29016):在android.os.Handler.handleCallback(Handler.java:587)
10-19 20:25:14.681:E / AndroidRuntime(29016):在android.os.Handler.dispatchMessage(Handler.java:92)
10-19 20:25:14.681:E / AndroidRuntime(29016):在android.os.Looper.loop(Looper.java:130)
10-19 20:25:14.681:E / AndroidRuntime(29016):在android.app.ActivityThread.main(ActivityThread.java:3701)
10-19 20:25:14.681:E / AndroidRuntime(29016):在java.lang.reflect.Method.invokeNative(本机方法)
10-19 20:25:14.681:E / AndroidRuntime(29016):在java.lang.reflect.Method.invoke(Method.java:507)
10-19 20:25:14.681:E / AndroidRuntime(29016):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:866)
10-19 20:25:14.681:E / AndroidRuntime(29016):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:624)
10-19 20:25:14.681:E / AndroidRuntime(29016):在dalvik.system.NativeStart.main(本机方法)
10-19 20:25:14.681:E / AndroidRuntime(29016):由:java.lang.reflect.InvocationTargetException
10-19 20:25:14.681:E / AndroidRuntime(29016):在java.lang.reflect.Method.invokeNative(本机方法)
10-19 20:25:14.681:E / AndroidRuntime(29016):在java.lang.reflect.Method.invoke(Method.java:507)
10-19 20:25:14.681:E / AndroidRuntime(29016):在android.view.View $ 1.onClick(View.java:2163)
10-19 20:25:14.681:E / AndroidRuntime(29016):... 11个
10-19 20:25:14.681:E / AndroidRuntime(29016):java.io.IOException异常:产生的原因权限被拒绝
10-19 20:25:14.681:E / AndroidRuntime(29016):在java.io.File.createNewFileImpl(本机方法)
10-19 20:25:14.681:E / AndroidRuntime(29016):在java.io.File.createNewFile(File.java:1257)
10-19 20:25:14.681:E / AndroidRuntime(29016):在org.brotheroftux.depositmaster.MainActivity.btnsave_clicked(MainActivity.java:43)
10-19 20:25:14.681:E / AndroidRuntime(29016):... 14个


解决方案

从<一个href=\"http://developer.android.com/reference/android/os/Environment.html#getExternalStorageDirectory%28%29\"相对=nofollow> Android的环境DOC


  

应用程序不应该直接使用此顶级目录,以避免污染用户的根命名空间。这是私人的应用程序的任何文件应该被放置在由Context.getExternalFilesDir返回一个目录,该系统会在应用程序卸载删除照顾。其他共享文件应该被放置在由getExternalStoragePublicDirectory(字符串)返回目录之一。


在换句话说,这是不是一个真正的好的做法(虽然也许你有一些很好的理由,背景不是很清楚)。

此外,作为邪神和特德·霍普评论道,确保权限是在清单中的正确位置。

I'm creating/saving file on SD card. 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

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. Other shared files should be placed in one of the directories returned by 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.

这篇关于安卓:IOException异常:权限被拒绝的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆