[英]How can i create file at external storage(sdcard) in Android 10?
I try to make 'File Control' app.我尝试制作“文件控制”应用程序。
I found how to read file, but I can't find how to write file.我找到了如何读取文件,但我找不到如何写入文件。
I set permission like this.我这样设置权限。
AndroidManifast.xml AndroidManifast.xml
<application
...
android:requestLegacyExternalStorage="true"
...
</application>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
and i call this code in MainActivity.onCreate.我在 MainActivity.onCreate 中调用此代码。
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
public static void verifyStoragePermissions(Activity activity) {
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(activity, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
activity,
PERMISSIONS_STORAGE,
REQUEST_EXTERNAL_STORAGE
);
}
}
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, MODE_PRIVATE);
verifyStoragePermissions(this);
I want read and write file, at /storage/3066-3133/title.txt
.我想要读取和写入文件,位于
/storage/3066-3133/title.txt
。 /storage/3066-3133
is my sdcard path. /storage/3066-3133
是我的 SD 卡路径。
read file is work perfectly.读取文件工作正常。
public void readFile() {
String fileTitle = "title.txt";
File file = new File("/storage/3066-3133", fileTitle);
try {
BufferedReader reader = new BufferedReader(new FileReader(file));
String result = "";
String line;
while ((line = reader.readLine()) != null) {
result += line;
}
popupToast("read : " + result);
reader.close();
} catch (FileNotFoundException e1) {
e1.printStackTrace();
popupToast("no file found");
} catch (IOException e2) {
e2.printStackTrace();
popupToast("read fail");
}
}
but write code occur exception.但是写代码出现异常。
write Exception: /storage/3066-3133/title.txt: open failed: EACCES (Permission denied)
public void writeFile() {
String fileTitle = "title.txt";
File file = new File("/storage/3066-3133", fileTitle);
try {
if (!file.exists()) {
file.createNewFile(); // error!!!!
}
FileWriter writer = new FileWriter(file, false);
String str = " write text ";
writer.write(str);
writer.close();
} catch (IOException e) {
e.printStackTrace();
popupToast("write fail");
}
}
Plus, I can write file at /storage/emulated/0/title.txt
.另外,我可以在
/storage/emulated/0/title.txt
写入文件。 /storage/emulated/0
is bullt-in external storage path. /storage/emulated/0
是内置的外部存储路径。
I need more permission for sdcard?我需要更多的 sdcard 权限?
or this is problem?或者这是问题? First, i create project for target sdk:31.
首先,我为目标 sdk:31 创建项目。 and i change 29 in build.gradle.
我在 build.gradle 中更改了 29。 changing target sdk is not work?
更改目标 sdk 不起作用? Someone tell sdk 30 little changed.
有人告诉 sdk 30 变化不大。 but, if this is problem, i can't understand then why 'read' work...
但是,如果这是问题,我不明白为什么“阅读”工作......
USE利用
uses-permission android:name="android.permission.MANAGE_EXTERNAL_STORAGE" in your manifest along with
在清单中使用许可 android:name="android.permission.MANAGE_EXTERNAL_STORAGE" 以及
android:requestLegacyExternalStorage="true" in your
<
application/>
android:requestLegacyExternalStorage="true" 在您的
<
应用程序/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.