获得 java.lang.SecurityException:不受支持的路径。但我正在申请权限

huangapple go评论71阅读模式
英文:

Getting java.lang.SecurityException: Unsupported path. But I am asking for permissions

问题

我正在使用DownloadManager将文件下载到一个目录,该目录不是我的“本地软件包”目录的一部分,即位于/storage/emulated/0/Android/data/myPackageName/之外,而是属于另一个目录的软件包,更具体地说,是obb目录,位于/storage/emulated/0/Android/obb/otherPackageName/

如果我安装在“我的软件包目录”上,没有问题,但如果我超出这个目录,我会收到一个“java.lang.SecurityException: 不支持的路径 /storage/emulated/0/Android/obb/otherPackageName/fileName.obb”

我在Android清单中具有权限,WRITE_EXTERNAL_STORAGE,READ_EXTERNAL_STORAGE,并且我使用ContextCompat.checkSelfPermission来检查是否拥有这些权限,并查看它是否等于PackageManager.PERMISSION_GRANTED,确实如此。但是,如果没有,对于API级别>=23,我会使用类似于以下的内容

requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, 
    Manifest.permission.READ_EXTERNAL_STORAGE}, CODE);

然后我使用onRequestPermissionsResult()来检查响应并查看是否为PackageManager.PERMISSION_GRANTED,以便我可以开始下载。

即使有了这个,我还是遇到了异常。这可能是特定于设备还是特定于API吗?我将不得不在其他设备和API上进行测试,以确保。
我在这里做错了什么吗?我需要额外的权限吗?在目录之外写入是可能的吗?

另外,我是通过Environment.getExternalStorageDirectory()来获取外部目录的,然后我将子目录添加为字符串(对于/storage/emulated/0/Android/data/myPackageName/...是有效的,但对于/storage/emulated/0/Android/obb/otherPackageName/...却无效)

谢谢阅读,如果您需要任何信息,请告诉我。

英文:

I am downloading files using DownloadManager to a directory, that isn't apart of my "local packages" directory, i.e. outside /storage/emulated/0/Android/data/myPackageName/, but part of another directory's package, more specifically, obb directory, /storage/emulated/0/Android/obb/otherPackageName/

If I install on "my package's directory", no problem, if I got outside of it, I get a "java.lang.SecurityException: Unsupported path /storage/emulated/0/Android/obb/otherPackageName/fileName.obb"

I have my permissions in the android manifest, WRITE_EXTERNAL_STORAGE, READ_EXTERNAL_STORAGE, and I check if I have them using ContextCompat.checkSelfPermission and see if it equals PackageManager.PERMISSION_GRANTED, which it does. But if it didn't, and for API level>=23, I use something along the lines of

requestPermissions(new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, 
    Manifest.permission.READ_EXTERNAL_STORAGE}, CODE);

And then I use onRequestPermissionsResult() to check the response and see if it is a PackageManager.PERMISSION_GRANTED, so that I may be able to start the download.

Even with this, I am getting the exception. Can this be device specific or API specific? I'll have to test on other devices and API's to know for sure.
Am I doing something wrong here? Do I need an extra permission? It has to be possible to write outside the directory?

Also, I am getting the external directory using Environment.getExternalStorageDirectory(), and then I add the subdirectories as a string (works for /storage/emulated/0/Android/data/myPackageName/..., but not for /storage/emulated/0/Android/obb/otherPackageName/...)

Thank you for reading, lemme know if you need any information.

答案1

得分: 2

我之前是直接将文件存储在我的包目录之外,异常是在DownloadManager内部抛出的。
因此,我开始将文件存储在“本地”目录中,然后手动将其移动到我想要的目录。

你可以参考这个回答,了解一个移动文件的示例方法:
https://stackoverflow.com/a/11327789/13416944

由于我仍在使用Environment.getExternalFilesDirectory(),而在这些较新的API中,这并不是最好的做法,一旦我找到更好的解决方案,我希望能编辑这个答案。

英文:

I was directly storing the file outside my package's directory, and the exception was being thrown inside DownloadManager.
So I started storing the file in the "local" directory, then manually moved it to the directory I wanted.

You can see this answer for an example method to move files:
https://stackoverflow.com/a/11327789/13416944

Since I'm still using Environment.getExternalFilesDirectory(), and that's not really the best way to do it in these newer API's, I'll hopefully edit this answer once I find a better solution

答案2

得分: 1

我在 Android 版本 10 及以上遇到了这个错误,但在较低版本中一切正常。
对于 Android 10 及以上版本,我通过为下载管理器指定 subPath 来解决了这个问题,这似乎在 10 及以上版本中是必需的:

var request = DownloadManager.Request(uri)
var filename = "MyApp" + imageId + "_.jpg";
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES, "your-sub-path")

请注意,subpath 是您下载文件的名称,例如:landschaft.jpg

** 不要忘记添加运行时权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
英文:

I got this error in androids version above 10, But in lower Version everything is fine.
For android above 10 i solve this problem by specifying subPath for Download manager and it seems necessary for version 10 and above:

 var request = DownloadManager.Request(uri)
 var filename = &quot;MyApp&quot; + imageId + &quot;_.jpg&quot;
 request.setDestinationInExternalPublicDir(Environment.DIRECTORY_PICTURES,&quot;your-sub-path&quot;)

Note that subpath is name of your downloaded file for example : landschaft.jpg

** Do not forget to add runtime Persmission:

&lt;uses-permission android:name=&quot;android.permission.WRITE_EXTERNAL_STORAGE&quot; /&gt;

huangapple
  • 本文由 发表于 2020年8月18日 18:15:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/63466516.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定