我想在我的Android应用中写入外部存储,但我似乎无法获得正确的权限。该应用程序在Android 7上运行,因此我在运行时请求权限,如下所示:
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
int ACCESS_EXTERNAL_STORAGE_STATE = 1;
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
ACCESS_EXTERNAL_STORAGE_STATE);
}
permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED) {
// We don't have permission so prompt the user
int ACCESS_EXTERNAL_STORAGE_STATE = 1;
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
ACCESS_EXTERNAL_STORAGE_STATE);
}
...
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode)
{
case 1: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED)
{
//reload my activity with permission granted or use the features what required the permission
} else
{
Toast.makeText(this, "The app was not allowed to write to your storage. Hence, it cannot function properly. Please consider granting it this permission", Toast.LENGTH_LONG).show();
}
}
}
}
我做的一个观察是onRequestPermissionsResult似乎返回没有授予权限,但是在启动应用程序以允许写入外部存储时我也从未被提示过。
我在Manifest中设置了这些权限:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<application>
写入存储的代码在这里:
public static void appendByteBuffer(byte[] data)
{
String filename = Environment.getExternalStorageDirectory().getAbsolutePath()+"/test.raw";
//filename value is "/storage/emulated/0/test.raw"
File rawAudioFile = new File(filename);
if (!rawAudioFile.exists())
{
try
{
rawAudioFile.createNewFile();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try (FileOutputStream output = new FileOutputStream(filename, true)) {
output.write(data);
} catch (IOException e) {}
}
03-16 14:50:49.988 19145-19306 /? W / System.err:java.io.IOException: 许可被拒绝03-16 14:50:49.988 19145-19306 /? W / System.err的:
在java.io.UnixFileSystem.createFileExclusively0(本机方法)03-16 14:50:49.988 19145-19306 /? W / System.err:at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280) 03-16 14:50:49.989 19145-19306 /? W / System.err:at java.io.File.createNewFile(File.java:948)03-16 14:50:49.989 19145-19306 /? W / System.err:at com.meeter.study.AppUtils.appendByteBuffer(AppUtils.java:76)03-16 14:50:49.989 19145-19306 /? W / System.err:at
答案 0 :(得分:2)
- delete:
tableName: B
where: COD_A = (SELECT COD_A FROM ${database.defaultSchemaName}.A WHERE name = 'A_NAME')
- delete:
tableName: A
where: name = 'A_NAME'
使用local i = 1
local function Move()
transition.to(player, {time=timeTable[i+1], x=TableX[i+1], y=TableY[i+1]})
i=i+1
end
timer.performWithDelay( timeTable[i], Move, 0 )
代替ContextCompat
:
替换:
ActivityCompat
使用:
checkSelfPermission
然后,添加permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
permission = ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);
方法来处理ALLOW,DENY选择。希望这会有所帮助。
permission = ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE);
permission = ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE);