gpt4 book ai didi

java - 在存储中保存文本的权限android不工作

转载 作者:行者123 更新时间:2023-12-01 19:07:22 24 4
gpt4 key购买 nike

我已经构建了示例应用程序,供用户编写一些文本并将其保存在内部存储上。

格式为 TXT 或 pdf。我已经尝试过 android Kitkat sdk 19 并且工作正常,但是当我尝试最新的 android 9 时,我被拒绝无法将文本保存在内部存储中

编辑:我想知道是否必须手动创建“我的文件夹”

我缺少什么?

enter image description here

AndroidManifest

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

主要 Activity

    protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mSaveTextBtn.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
mText = mResultEt.getText().toString().trim();
if(mText.isEmpty()){
Toast.makeText(MainActivity.this, "write text First...", Toast.LENGTH_SHORT).show();
}
else{
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
if(checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED){
String[] permissions = {Manifest.permission.WRITE_EXTERNAL_STORAGE};
requestPermissions(permissions,WRITE_EXTERNAL_STORAGE_CODE);
}else {
saveToTxtFile(mText);
}
}else {
saveToTxtFile(mText);
}
}

}
});

}

@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode){
case WRITE_EXTERNAL_STORAGE_CODE: {
if (grantResults.length > 0 && grantResults[0]
== PackageManager.PERMISSION_GRANTED) {
//permission granted save data
saveToTxtFile(mText);
} else {
//permission denied
Toast.makeText(this, "Storage Permission required to store", Toast.LENGTH_SHORT).show();
}
}
break;
}
}

private void saveToTxtFile(String mText){
//get current time for file name
String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss",
Locale.getDefault()).format(System.currentTimeMillis());
try{
//path tp storage
File path = Environment.getExternalStorageDirectory();
//create folder named "My file"
File dir = new File( path + "/My Files/");
dir.mkdirs();
//file name
String fileName = "MyFile_" + timestamp + ".txt";

File file = new File (dir, fileName);

//used to store characater in file
FileWriter fw = new FileWriter(file.getAbsoluteFile());
BufferedWriter bw = new BufferedWriter(fw);
bw.write(mText);
bw.close();

//show file name and path where file saved
Toast.makeText(this, fileName+"is saved to\n" +dir, Toast.LENGTH_SHORT).show();

}catch (Exception e){
//if anything goes wrong
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}

最佳答案

正如建议的 Shashanthkashyap答案。

这只是一个解决方法,但它确实有效。

我已添加

android:requestLegacyExternalStorage="true"

给我的<application> AndroidManifest.xml 中的元素文件。

并更改了线路

File path = Environment.getExternalStorageDirectory();

File path = new File(getExternalFilesDir(null).getAbsolutePath());

关于java - 在存储中保存文本的权限android不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59525503/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com