gpt4 book ai didi

android - 打开失败: EACCES (Permission denied) api23?

转载 作者:行者123 更新时间:2023-12-02 17:22:18 26 4
gpt4 key购买 nike

我知道运行时权限,并且我已经考虑到了这一点,但它仍然不起作用。我已将这些行添加到 list 中:

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

这是我的onCreate方法:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new);
if (shouldAskPermissions()) {
askPermissions();
}
generateNoteOnSD(this,fileName,msg);
readFile(fileName);
}

这用于请求和授予权限

protected static boolean shouldAskPermissions() {
return (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP_MR1);
}

@TargetApi(23)
protected void askPermissions() {
String[] permissions = {
"android.permission.READ_EXTERNAL_STORAGE",
"android.permission.WRITE_EXTERNAL_STORAGE"
};
/* int requestCode = 200;
requestPermissions(permissions, requestCode);*/
// Check if we have write permission
int permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);

if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
ActivityCompat.requestPermissions(
this, permissions, 1);
}
}

这些是写作和阅读方法

 public static void generateNoteOnSD(Context context, String sFileName, String sBody) {
try {
File root = new File(String.valueOf(Environment.getExternalStorageDirectory()));
Log.e("file path is ", String.valueOf(Environment.getExternalStorageDirectory()));
if (!root.exists()) {
root.mkdirs();
}
File gpxfile = new File(root, sFileName);
String separator = System.getProperty("line.separator");

FileWriter writer = new FileWriter(gpxfile.getAbsoluteFile(), true);
writer.append(separator); // this will add new line ;

//FileWriter writer = new FileWriter(gpxfile);
writer.append(sBody);
writer.flush();
writer.close();
Toast.makeText(context, "Saved", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
public void readFile(String file1){
//Find the directory for the SD Card using the API
//*Don't* hardcode "/sdcard"
File sdcard = Environment.getExternalStorageDirectory();

//Get the text file
File file = new File(sdcard,file1);

//Read text from file
StringBuilder text = new StringBuilder();

try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;

while ((line = br.readLine()) != null) {
text.append(line);
text.append('\n');
}
br.close();
}
catch (IOException e) {
//You'll need to add proper error handling here
}

//Find the view by its id
Log.e("text",text.toString());
}

编辑这是我的堆栈跟踪:

W/System.err: java.io.FileNotFoundException: /storage/emulated/0/CrowdSensingData: open failed: EACCES (Permission denied)
W/System.err: at libcore.io.IoBridge.open(IoBridge.java:487)
W/System.err: at java.io.FileOutputStream.<init>(FileOutputStream.java:87)
W/System.err: at java.io.FileWriter.<init>(FileWriter.java:58)
W/System.err: at enis.example.com.alarmmanagertest.FileAndServerHandlingActivity.generateNoteOnSD(FileAndServerHandlingActivity.java:128)
W/System.err: at enis.example.com.alarmmanagertest.ActivityRecognitionService.handleDetectedActivities(ActivityRecognitionService.java:107)
W/System.err: at enis.example.com.alarmmanagertest.ActivityRecognitionService.onStart(ActivityRecognitionService.java:122)
W/System.err: at android.app.Service.onStartCommand(Service.java:459)
W/System.err: at android.app.ActivityThread.handleServiceArgs(ActivityThread.java:3209)
W/System.err: at android.app.ActivityThread.-wrap17(ActivityThread.java)
W/System.err: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1586)
W/System.err: at android.os.Handler.dispatchMessage(Handler.java:111)
W/System.err: at android.os.Looper.loop(Looper.java:207)
W/System.err: at android.app.ActivityThread.main(ActivityThread.java:5728)
W/System.err: at java.lang.reflect.Method.invoke(Native Method)
W/System.err: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
W/System.err: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:679)
W/System.err: Caused by: android.system.ErrnoException: open failed: EACCES (Permission denied)
W/System.err: at libcore.io.Posix.open(Native Method)
W/System.err: at libcore.io.BlockGuardOs.open(BlockGuardOs.java:186)
W/System.err: at libcore.io.IoBridge.open(IoBridge.java:473)

最佳答案

如果尚未授予权限,则不应立即调用需要它们的方法。而是在 onRequestPermissionsResult 中调用它们。这保证了它们只有在用户授予权限后才会被执行。

if (shouldAskPermissions()) {
askPermissions();
} else {
generateNoteOnSD(this,fileName,msg);
readFile(fileName);
}

@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
switch (requestCode) {
case 1: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
generateNoteOnSD(this,fileName,msg);
readFile(fileName);
}
}
}
}

编辑:此外,包括ContextCompat.checkSelfPermission在你的 shouldAskPermissions() 方法中。

编辑 2:这是 shouldAskPermissions() 的实现

protected boolean shouldAskPermissions() {
if(Build.VERSION.SDK_INT <= Build.VERSION_CODES.LOLLIPOP_MR1) {
return false;
}
int permission = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
return (permission != PackageManager.PERMISSION_GRANTED);
}

关于android - 打开失败: EACCES (Permission denied) api23?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42354236/

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