即使具有读/写权限,也无法从外部存储读取或创建文件

时间:2020-10-23 06:45:47

标签: java android file-io android-permissions

这是清单文件:

yourservice.subscribe((e: HttpEvent<any>) => {
          switch (e.type) {
            case HttpEventType.Sent:
              break;
            case HttpEventType.ResponseHeader:
              break;
            case HttpEventType.UploadProgress:
              this.progress = Math.round(e.loaded / e.total * 100);
              break;
            case HttpEventType.Response:
              this.progress = -1;
          }
    });

我使用过许可。 按日期命名的文本文件(例如:2020_10_23.txt) 来自外部存储(例如:存储/仿真/0/myDiary/2020_10_23.txt) 用于I / O操作。

此处是mainactivity中的核心代码:

const percent = progress.percent;
this.processPercentage.next(percent);

Android版本:11

应用权限:

enter image description here

这是logcat:

subscription:Subscription;
constructor(private uploadService: UploadService,private service:Service) {}
ngOnInit(){
 this.getProgress();
}

getProgress(){
 this.subscription = 
 this.service.processPercentage.asObservable().susbcribe((progress) => {
  this.progress = progress;
  console.log(this.progress);
  // You can bind to your progressbar from here 
 });
}

ngOnDestroy(){
 this.subscription.unsubscribe();
}

它无法在存储/模拟/ 0上创建文件夹“ myDiary” 我尽力了。为什么即使给了外部存储I / O权限也不能创建文件夹,文件?

2 个答案:

答案 0 :(得分:0)

使用此

public class Folder
{
    private final File appDataFolder;

    public Folder()
    {
        File file = Environment.getExternalStorageDirectory();

        appDataFolder = new File(file, "AppDataFolder");

        if (!appDataFolder.exists()) appDataFolder.mkdir();
    }

    public String getMainFolder()
    {
        return appDataFolder.getAbsolutePath();
    }

    public void deleteMainFolder()
    {
        if (appDataFolder.isDirectory())
        {
            String[] children = appDataFolder.list();

            for (String child : children)
            {
                new File(appDataFolder, child).delete();
            }
        }

        new Folder();
    }

    public void createNewFolder(String folderName)
    {
        File file = new File(appDataFolder, folderName);

        if (!file.exists()) file.mkdir();
    }

    public void createChildFolder(String parentName, String childName)
    {
        String path = appDataFolder.getAbsolutePath() + File.separator + parentName;

        File parent = new File(path);

        File file = new File(parent, childName);

        if (!file.exists()) file.mkdir();
    }
}

活动中

PermissionListener permissionlistener = new PermissionListener() {
        @Override
        public void onPermissionGranted() {
            new Folder().createNewFolder("myDiary");
            Toast.makeText(MainActivity.this, "Permission granted", Toast.LENGTH_SHORT).show();
        }

        @Override
        public void onPermissionDenied(List<String> deniedPermissions) {
            Toast.makeText(MainActivity.this, "Permission denied\n" + deniedPermissions.toString(), Toast.LENGTH_SHORT).show();
        }
    };

答案 1 :(得分:0)

在Android 11上,您无权访问外部存储的根目录。

您不能在/ storage / emulated / 0中创建文件或文件夹。

但是..您可以像/ p>在/ storage / emulated / 0上写入通常的公用文件夹

Download, Documents, DCIM, Pictures, Alarms, a.s.o.

查看设备上已经存在的文件夹,您通常可以将它们用于读取和/或写入操作。