简体   繁体   English

如何在Android上添加写入文件权限

[英]How to add Write to File Permission on Android

I want to write in a file by using the following: 我想使用以下内容写入文件:

 BufferedWriter buf = new BufferedWriter(new FileWriter("test2"));
 buf.write(mytext);
 buf.close();

but i got this message (file test2 read only) 但我收到此消息(file test2 read only)

How can i get the write permission? 我怎样才能获得写入权限?

Have you got this in your AndroidManifest.xml file? 你在AndroidManifest.xml文件中有这个吗?

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

Also, this link has everything you need to know about reading and writing files: 此外,此链接包含有关读取和写入文件的所有信息:

http://www.anddev.org/working_with_files-t115.html http://www.anddev.org/working_with_files-t115.html

byte[] data;

give imagebyte value to data variable 将imagebyte值赋给数据变量

 // Write to SD Card
FileOutputStream outStream = new FileOutputStream(String.format("/sdcard/piyush.jpg",
            System.currentTimeMillis())); 
        outStream.write(data);
        outStream.close();

Give Write Permission Open AndroidManifest.xml file and put this code 给写权限打开AndroidManifest.xml文件并放入此代码

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

This question is quite old but I am going to write this answer anyway since this is the top result when you search for 'write file permission in android' 这个问题已经很老了,但无论如何我都会写这个答案,因为当你搜索'在android中写入文件权限'时,这是最好的结果

NotACleverMan's answer is valid but since the release of MarshMallow, users must be asked to provide the permission manually. NotACleverMan的答案是有效的,但自MarshMallow发布以来,必须要求用户手动提供权限。 Therefore, the user must be presented a dialogue where he can explicitly deny or grant the permission for using some functionality. 因此,必须向用户呈现对话,在该对话中他可以明确拒绝或授予使用某些功能的许可。

Taken from the Android developer docs 取自Android开发人员文档

Beginning in Android 6.0 (API level 23), users grant permissions to apps while the app is running, not when they install the app. 从Android 6.0(API级别23)开始,用户在应用程序运行时向应用程序授予权限,而不是在安装应用程序时。 This approach streamlines the app install process, since the user does not need to grant permissions when they install or update the app. 此方法简化了应用安装过程,因为用户在安装或更新应用时无需授予权限。 It also gives the user more control over the app's functionality; 它还使用户可以更好地控制应用程序的功能; for example, a user could choose to give a camera app access to the camera but not to the device location. 例如,用户可以选择让相机应用程序访问相机,但不能访问设备位置。 The user can revoke the permissions at any time, by going to the app's Settings screen. 用户可以通过转到应用程序的“设置”屏幕随时撤消权限。

So, first you must include this in your manifest file 因此,首先必须将其包含在清单文件中

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

Thereafter, you must request for permission from the user when you need to access the specified resource 此后,您需要在需要访问指定资源时请求用户的许可

ActivityCompat.requestPermissions(thisActivity,
            new String[]{Manifest.permission.WRITE_STORAGE},
            YOUR_PERMISSION_STATIC_CODE_IDENTIFIER);

For more details on the implementation, refer this 有关实现的更多详细信息,请参阅此处

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

相关问题 Android 6.0文件写入权限被拒绝 - Android 6.0 File write permission denied 即使在授予权限后,Android 文件写入权限被拒绝错误 - Android File Write Permission Denied Error even after granting the permission 尝试将文件写入Android中的SD卡时,FileNotFoundException(权限被拒绝) - FileNotFoundException (permission denied) when trying to write file to sdcard in Android 如何在设备android上添加文件访问权限 - How to add files access permission on device android 如何在 android SDK 33 中访问权限 READ_INTERNAL_STORAGE 和 WRITE_INTERNAL_STORAGE 文件(pdf 文档) - How to access permission READ_INTERNAL_STORAGE and WRITE_INTERNAL_STORAGE file (pdf docs)in android SDK 33 如何在不要求运行时权限的情况下在Android 6.0中写入SD卡? - How to write to SD Card in Android 6.0 without asking runtime permission? 如何在 Android 6.0+ 中获得将文件写入 SDCard 的权限 - How to get permission to write files into SDCard in Android 6.0+ 无法在 Android 上获得写入权限 - Unable to get Write permission on Android 如何为applet提供在文件系统上写入的访问权限 - how to provide access permission for applet to write on file system 如何在jdk1.4中为applet提供文件写入权限 - how to provide file write permission for applet in jdk1.4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM