如何在内部存储(如WhatsApp或Allo)中创建文件夹?
问题描述:
我有一个任务将位图保存在内部存储器中.我尝试了所有方式.我无法获得所需的输出.在文件夹中创建并保存在内部设备中的文件夹数据/用户/0/数据/~~ .--tried
I have a task to save the bitmap in internal storage. I tried in all ways. I can't get the required output. The folder created and saved in internal device in folder data/user/0/data/~~. --tried
File mediaFile = new File(c.getExternalCacheDir(), "/MaherSaad");
工作并在缓存中创建文件夹-android/data/com.myapp.〜/cache
worked and create folder in cache -- android/data/com.myapp.~/cache
我想创建文件夹并像这样
i want to create folder and appear like that
内部存储空间/Myapp/newFolder
答
最终找到了解决方案在数据文件夹之外创建文件夹
finally found the solution creating a folder outside data folder
简单地
File mFolder = new File(Environment.getExternalStorageDirectory(), "Folder_Name");
if (!mFolder.exists()) {
boolean b = mFolder.mkdirs();
}
该错误未处理运行时权限代码简单...
the mistake is not handling the Run time permission code simply ...
String TAG = "Permsission : ";
if (Build.VERSION.SDK_INT >= 23) {
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v(TAG,"Permission is granted");
return true;
} else {
Log.v(TAG,"Permission is revoked");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
return false;
}
}
else { //permission is automatically granted on sdk<23 upon installation
Log.v(TAG,"Permission is granted");
return true;
}
处理权限
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
String TAG = "Permsission : ";
if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
Log.v(TAG,"Permission: "+permissions[0]+ "was "+grantResults[0]);
//resume tasks needing this permission
//Define the path you want
File mFolder = new File(Environment.getExternalStorageDirectory(), "Folder_Name");
if (!mFolder.exists()) {
boolean b = mFolder.mkdirs();
}
}
}