如何在内部存储中创建文件夹并将位图图像保存在Android棉花糖上的文件夹中。 我想特别了解棉花糖...而且我已将所有许可授予清单文件。 请帮我解决这个问题。
答案 0 :(得分:0)
参考:How to check Grants Permissions at Run-Time?
我刚刚发现了我的需要,我们可以检查该权限是否由授予:
checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
必要时请求权限
if (checkSelfPermission(Manifest.permission.WRITE_EXTERNAL_STORAGE)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
100);
// MY_PERMISSIONS_REQUEST_READ_CONTACTS is an
// app-defined int constant
return;
}
Handle the permissions request response
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
switch (requestCode) {
case 100: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// permission was granted, yay! do the
// calendar task you need to do.
} else {
// permission denied, boo! Disable the
// functionality that depends on this permission.
}
return;
}
// other 'switch' lines to check for other
// permissions this app might request
}
}
创建类似文件夹
File directory = new File(Environment.getExternalStorageDirectory()+File.separator+"images");
directory.mkdirs();
让我知道这是否适合您!
您还需要在AndroidManifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
答案 1 :(得分:0)
在特别的棉花糖版本中,需要以编程方式获取许可。以下是读取外部存储器的示例代码。
ActivityCompat.requestPermissions(MainActivity.this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
PERMISSION_CODE);
在onResultPermissionsRequest()方法中
@Override
public void onRequestPermissionsResult(int requestCode,
String permissions[], int[] grantResults) {
if(requestCode == PERMISSION_CODE){
if(grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED){
//Permission granted
}
else{
//Permission Not granted
}
}
}
希望为各种不同的权限编写类似的代码。希望这应该工作。万一这行不通,那就确保棉花糖中需要动态获取权限。