目录创build不起作用在棉花糖Android
我在我的本地应用程序创build目录。它在Android 5.0中完美工作,但在Android 6.0(棉花糖)有时它的工作有时不是。我使用下面的代码来创build新的目录。我保存video和图像文件在目录中。
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(); }
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; } } }
运行在Android M上的设备,你不能直接写入数据到存储。 你需要征得许可。
这个链接可能会有所帮助
所以应该在运行时询问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()callback方法。 你可以在这里添加逻辑。
@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. } }
希望它有助于:)
在Android Marshmallow中,您需要在运行时请求包括STORAGE权限组在内的危险权限 。 我知道一个不错的图书馆,可以让你的生活更轻松, RxPermissions 。
您可以使用less量代码来获得许可:
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 } });