Java Android在内部存储中创建目录在Android 7.0中不起作用?
如果android中不存在具有权限的目录,我将在async中使用此代码创建目录。我的代码在安卓5.1中成功运行,但当我在安卓7.0中部署应用程序时,该目录不会自动创建Java Android在内部存储中创建目录在Android 7.0中不起作用?,java,android,android-studio,Java,Android,Android Studio,如果android中不存在具有权限的目录,我将在async中使用此代码创建目录。我的代码在安卓5.1中成功运行,但当我在安卓7.0中部署应用程序时,该目录不会自动创建 File sdcard = Environment.getExternalStorageDirectory() ; File folder = new File(sdcard.getAbsoluteFile(), "Quoteimages"); if(!folder.exists()){
File sdcard = Environment.getExternalStorageDirectory() ;
File folder = new File(sdcard.getAbsoluteFile(), "Quoteimages");
if(!folder.exists()){
folder.mkdir();
}
清单文件是
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_INTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE" />
链接到应用程序apk的详细信息
现在我该怎么做我想创建文件夹写图片从url到它,然后读取它们。这在android 5.1中适用,但在android版本7中不适用。您需要为棒棒糖以上的android版本实现运行时权限 也许这段代码可以帮助您:
private static final int PERMS_REQUEST_CODE = 123;
//徖
private boolean hasPermissions(){
int res = 0;
//string array of permissions,
String[] permissions = new String[]
{android.Manifest.permission.WRITE_EXTERNAL_STORAGE};
for (String perms : permissions){
res = checkCallingOrSelfPermission(perms);
if (!(res == PackageManager.PERMISSION_GRANTED)){
return false;
}
}
return true;
}
private void requestPerms(){
String[] permissions = new String[]{android.Manifest.permission.WRITE_EXTERNAL_STORAGE};
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){
requestPermissions(permissions,PERMS_REQUEST_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
boolean allowed = true;
switch (requestCode){
case PERMS_REQUEST_CODE:
for (int res : grantResults){
// if user granted all permissions.
allowed = allowed && (res == PackageManager.PERMISSION_GRANTED);
}
break;
default:
// if user not granted permissions.
allowed = false;
break;
}
if (allowed){
//user granted all permissions we can perform our task.
ListItem listItem = new ListItem();
Glide.with(getApplicationContext()).asBitmap().load(listItem.getImgurl()).into(new SimpleTarget<Bitmap>(){
@Override
public void onResourceReady(Bitmap resource, Transition<? super Bitmap> transition) {
FileOutputStream fileOutputStream = null;
File file = getDisc();
if(!file.exists()&& !file.mkdirs()){
Toast.makeText(getApplicationContext(),"Can't create directory to save image", Toast.LENGTH_LONG).show();
return;
}
SimpleDateFormat simpleDataFormat = new SimpleDateFormat("yyyymmsshhmmss");
String date = simpleDataFormat.format(new Date());
String name = "img"+date+".jpg";
String file_name = file.getAbsolutePath()+"/"+name;
File new_file= new File(file_name);
try {
fileOutputStream=new FileOutputStream(new_file);
resource.getHeight();
resource.getWidth();
resource.compress(Bitmap.CompressFormat.JPEG, 100, fileOutputStream);
Toast.makeText(getApplicationContext(),"SAVED", Toast.LENGTH_LONG).show();
fileOutputStream.flush();
fileOutputStream.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
refreshGallery(new_file);
}
public void refreshGallery(File file){
Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
intent.setData(Uri.fromFile(file));
sendBroadcast(intent);
}
private File getDisc(){
File file = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
return new File(file, "FolderNamerYourChoice");
}
});
}
else {
// we will give warning to user that they haven't granted permissions.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (shouldShowRequestPermissionRationale(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)){
Toast.makeText(this, "Storage Permissions denied.", Toast.LENGTH_SHORT).show();
}
}
}
private boolean hasPermissions(){
int res=0;
//权限的字符串数组,
字符串[]权限=新字符串[]
{android.Manifest.permission.WRITE_EXTERNAL_STORAGE};
for(字符串perms:权限){
res=checkCallingOrSelfPermission(perms);
如果(!(res==PackageManager.PERMISSION\u已授予)){
返回false;
}
}
返回true;
}
私有void requestPerms(){
字符串[]权限=新字符串[]{android.Manifest.permission.WRITE\u EXTERNAL\u STORAGE};
if(Build.VERSION.SDK\u INT>=Build.VERSION\u code.M){
请求权限(权限、权限请求代码);
}
}
@凌驾
public void onRequestPermissionsResult(int-requestCode,@NonNull-String[]permissions,@NonNull-int[]grantResults){
允许布尔值=真;
开关(请求代码){
案例PERMS\U请求\U代码:
对于(int res:grantResults){
//如果用户授予了所有权限。
allowed=allowed&(res==PackageManager.PERMISSION\u provided);
}
打破
违约:
//如果用户未被授予权限。
允许=错误;
打破
}
如果(允许){
//用户授予了我们可以执行任务的所有权限。
ListItem ListItem=新ListItem();
Glide.with(getApplicationContext()).asBitmap().load(listItem.getImgurl()).into(new SimpleTarget()){
@凌驾
public void onResourceReady(位图资源,Transitional)因此,没有名为READ\u INTERNAL\u STORAGE
或WRITE\u INTERNAL\u STORAGE
的权限。android 7.0的可能副本更改了应用程序的文件系统。