Java 如何在android应用程序中获得权限?
我在手机上获取从raw保存文件的权限时遇到问题。我已在清单中说明:Java 如何在android应用程序中获得权限?,java,android,Java,Android,我在手机上获取从raw保存文件的权限时遇到问题。我已在清单中说明: <uses-permission android:name="android.permissions.WRITE_EXTERNAL_STORAGE" /> <uses-permission android:name="android.permissions.READ_EXTERNAL_STORAGE" /> 要检查权限,请执行以下操作: private static final int REQUEST_
<uses-permission android:name="android.permissions.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permissions.READ_EXTERNAL_STORAGE" />
要检查权限,请执行以下操作:
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
);
}
}
当我试图通过单击应用程序中的按钮保存此文件时,我收到错误:
W/System.err:java.io.FileNotFoundException:/storage/emulated/0/Downloadtestsound.mp3:open失败:EACCES(权限被拒绝)
怎么了?我认为这是你的问题:
fout = new FileOutputStream(new File(downloadsDirectoryPath + filename));
请使用以下选项:
fout = new FileOutputStream(new File(downloadsDirectoryPath , filename));
您只有
/storage/emulated/0/Download/testsound.mp3
的权限,而没有/storage/emulated/0/Downloadtestsound.mp3
我相信这是您的问题:
fout = new FileOutputStream(new File(downloadsDirectoryPath + filename));
请使用以下选项:
fout = new FileOutputStream(new File(downloadsDirectoryPath , filename));
您只有/storage/emulated/0/Download/testsound.mp3
权限,而没有/storage/emulated/0/Downloadtestsound.mp3
放置“/”在filename
和downloadsDirectoryPath
之间,这就是它给出错误的原因。要测试您是否没有获得权限,请转到“设置”并在那里选中“手动授予权限”,然后重新启动
public void onClick(View v) {
InputStream in = null;
FileOutputStream fout = null;
try {
in = getResources().openRawResource(R.raw.testsound);
String downloadsDirectoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
String filename = "testsound.mp3";
fout = new FileOutputStream(new File(downloadsDirectoryPath + "/"+filename));
final byte data[] = new byte[1024];
int count;
while ((count = in.read(data, 0, 1024)) != -1) {
fout.write(data, 0, count);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fout != null) {
try {
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
将“/”放在filename
和downloadsDirectoryPath
之间,这就是它给出错误的原因。若要测试您是否未获得权限,请转到“设置”并在那里检查手动授予权限,然后再次启动
public void onClick(View v) {
InputStream in = null;
FileOutputStream fout = null;
try {
in = getResources().openRawResource(R.raw.testsound);
String downloadsDirectoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
String filename = "testsound.mp3";
fout = new FileOutputStream(new File(downloadsDirectoryPath + "/"+filename));
final byte data[] = new byte[1024];
int count;
while ((count = in.read(data, 0, 1024)) != -1) {
fout.write(data, 0, count);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fout != null) {
try {
fout.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
});
下载文件夹后是否缺少斜杠?换句话说,您没有根访问权限?下载文件夹后是否缺少斜杠?换句话说,您没有根访问权限hmmm,这会将错误更改为/storage/emulated/0/Download/testsound.mp3:打开失败:EACCES(权限被拒绝)@Johnystrawits您能尝试将您的目标sdk版本更改为21并再次检查吗?我将目标sdk设置为21,但仍然收到此错误:/Hmmm,将错误更改为/storage/simulated/0/Download/testsound.mp3:open失败:EACCES(权限被拒绝)@Johnystrawits能否尝试将您的目标sdk版本更改为21并再次检查?我已将目标sdk设置为21,但仍然出现以下错误:/