gpt4 book ai didi

Android - 创建新文件的权限被拒绝

转载 作者:太空狗 更新时间:2023-10-29 13:01:58 27 4
gpt4 key购买 nike

我试图在 Android 中创建一个文件,即使在遵循了所有其他答案之后,我仍然被拒绝许可。 saveImage() 方法出错。

 // STATICS
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};

 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState)
verifyStoragePermissions(this);
}

  //HERE IS THE METHOD THAT ASKS FOR PERMISSIONS
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
);
}
}

  @Override //ON REQUEST PERMISSION RESULT, 
//I ACCEPTED THE PERMISSION, THE CODE HERE WAS EXECUTED AS IT SHOULD
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {

case 1: {
Log.d(TAG,"THIS IS FOR THE WRITE/READ PERMISSIONS");
Log.d(TAG,grantResults.toString());
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
Log.d(TAG,"[Persmission accepted for writing/reading]");


// permission was granted, yay! Do the
// camera related task you need to do.
} else {
Log.d(TAG,"[Permission denied]");
// permission denied, boo! Disable the
// functionality that depends on this permission.
}

}

// other 'case' lines to check for other
// permissions this app might request.
}
}

 //THE METHOD WHERE THE ERROR HAPPENS
public String saveImage(Bitmap myBitmap) {

ByteArrayOutputStream bytes = new ByteArrayOutputStream();
myBitmap.compress(Bitmap.CompressFormat.JPEG, 90, bytes);

File wallpaperDirectory = new File(
Environment.getExternalStorageDirectory() + "TEST_APP_PHOTO_FOLDER");

// have the object build the directory structure, if needed.
if (!wallpaperDirectory.exists()) {

wallpaperDirectory.mkdirs();
}

try {

File f = new File(wallpaperDirectory + "testfile.jpg");

f.mkdirs();
f.createNewFile(); // <--- STACK TRACE POINTS HERE

FileOutputStream fo = new FileOutputStream(f);
..
..
..

错误发生在 saveImage() 方法中。我有评论解释它发生的地方。我已经尝试过使用 f.mkdirs() 的方法,但没有它,无论哪种方式都不起作用。两次权限都被拒绝。

堆栈跟踪

java.io.IOException: Permission denied
at java.io.UnixFileSystem.createFileExclusively0(Native Method)
at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:281)
at java.io.File.createNewFile(File.java:1000)
at car.andrej.tradingapp.ProfileActivity.ProfileActivity.saveImage(ProfileActivity.java:263)
at car.andrej.tradingapp.ProfileActivity.ProfileActivity.onActivityResult(ProfileActivity.java:220)
at android.app.Activity.dispatchActivityResult(Activity.java:7556)
at android.app.ActivityThread.deliverResults(ActivityThread.java:4487)
at android.app.ActivityThread.handleSendResult(ActivityThread.java:4534)
at android.app.ActivityThread.-wrap20(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1752)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6944)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

在 list 中:

 <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />

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

我错过了什么?...

最佳答案

这不是 Android 权限问题。相反,Exception 是由于 Linux 权限引起的,尽管是无意的。

在实例化 saveImage() 中使用的 File 时,您将新文件/目录的名称连接到 Environment.getExternalStorageDirectory();例如,对于 wallpaperDirectory:

new File(Environment.getExternalStorageDirectory() + "TEST_APP_PHOTO_FOLDER")

这是在 Environment.getExternalStorageDirectory() 上隐式调用 toString() 并直接将名称附加到它上面,中间没有必要的路径分隔符,所以你最终得到全名类似于 /storage/emulated/0TEST_APP_PHOTO_FOLDER。这指的是外部存储目录的父目录中的一个文件,您在那里没有写入权限,因此出现了Exception

只需更改您的 File 实例化,以使用采用单独的 FileString 参数的构造函数;第一个用于父目录,第二个用于名称。基本上,将加号更改为逗号。例如:

new File(Environment.getExternalStorageDirectory(), "TEST_APP_PHOTO_FOLDER")

关于Android - 创建新文件的权限被拒绝,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55501560/

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