我正在我的原生应用程序中创建目录。它在Android 5.0中完美运行,但在Android 6.0(Marshmallow)中有时它工作,有时没有。我正在使用以下代码创建新目录。我正在保存视频和目录中的图像文件。
public static void createApplicationFolder() {
File f = new File(Environment.getExternalStorageDirectory(), File.separator + Config.VIDEO_COMPRESSOR_APPLICATION_DIR_NAME);
f.mkdirs();
f = new File(Environment.getExternalStorageDirectory(), File.separator + Config.VIDEO_COMPRESSOR_APPLICATION_DIR_NAME + Config.VIDEO_COMPRESSOR_COMPRESSED_VIDEOS_DIR);
f.mkdirs();
f = new File(Environment.getExternalStorageDirectory(), File.separator + Config.VIDEO_COMPRESSOR_APPLICATION_DIR_NAME + Config.VIDEO_COMPRESSOR_TEMP_DIR);
f.mkdirs();
}
答案 0 :(得分:10)
public class MyDevIDS extends AppCompatActivity {
private static final int REQUEST_RUNTIME_PERMISSION = 123;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (CheckPermission(MyDevIDS.this, Manifest.permission.WRITE_EXTERNAL_STORAGE)) {
// you have permission go ahead
createApplicationFolder();
} else {
// you do not have permission go request runtime permissions
RequestPermission(MyDevIDS.this, Manifest.permission.WRITE_EXTERNAL_STORAGE, REQUEST_RUNTIME_PERMISSION);
}
}
@Override
public void onRequestPermissionsResult(int permsRequestCode, String[] permissions, int[] grantResults) {
switch (permsRequestCode) {
case REQUEST_RUNTIME_PERMISSION: {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
// you have permission go ahead
createApplicationFolder();
} else {
// you do not have permission show toast.
}
return;
}
}
}
public void RequestPermission(Activity thisActivity, String Permission, int Code) {
if (ContextCompat.checkSelfPermission(thisActivity,
Permission)
!= PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.shouldShowRequestPermissionRationale(thisActivity,
Permission)) {
} else {
ActivityCompat.requestPermissions(thisActivity,
new String[]{Permission},
Code);
}
}
}
public boolean CheckPermission(Context context, String Permission) {
if (ContextCompat.checkSelfPermission(context,
Permission) == PackageManager.PERMISSION_GRANTED) {
return true;
} else {
return false;
}
}
}
答案 1 :(得分:1)
在Android M上运行的设备,您无法直接将数据写入存储。你需要获得许可。
this链接可能会有所帮助
因此,应在运行时询问权限WRITE_EXTERNAL_STORAGE
。
public boolean permissions() {
//cheeck id device is Android M
if (Build.VERSION.SDK_INT >= 23) {
if(checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
//your permission is granted
return true;
} else {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
return false;
}
}
else {
//permission is automatically granted on devices lower than android M upon installation
return true;
}
}
您还需要覆盖 onRequestPermissionsResult()回调方法。你可以在这里添加逻辑。
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
//Add your logic of saving a file to directory.
}
}
希望有所帮助:)
答案 2 :(得分:0)
在Android Marshmallow中,您需要在运行时询问dangerous permissions,包括STORAGE权限组。 我知道一个漂亮的图书馆,可以让你的生活更轻松,RxPermissions。
您可以使用少量代码申请权限:
RxPermissions.getInstance(this)
.request(Manifest.permission.WRITE_EXTERNAL_STORAGE)
.subscribe(granted -> {
if (granted) { // Always true pre-M
// I can write to the storage now
} else {
// Oups permission denied
}
});