在Android P或更低版本中, 我们将以下代码用于android P资产提取路径,
public String getAppAssetsExtractingPath() {
return Environment.getExternalStorageDirectory()+"/" + _activity.getPackageName();
}
在此处,将文件的包装名称(将文件分组)添加到单个包装中。
在Android Q中, 为了提高用户隐私,不建议直接访问共享/外部存储设备。当应用程序定位到Build.VERSION_CODES.Q时,此方法返回的路径不再可供应用程序直接访问。 通过迁移到Context#getExternalFilesDir(String),MediaStore或Intent#ACTION_OPEN_DOCUMENT等替代方案,应用程序可以继续访问共享/外部存储中存储的内容。
public String getAppAssetsExtractingPath() {
Context context = _activity.getApplicationContext();
return context.getExternalFilesDir(null) +"/" + _activity.getPackageName();
}
但是在尝试以下代码后,在附加程序包名称时遇到了ENOENT错误
public String getAppAssetsExtractingPath() {
return context.getExternalFilesDir(null) + "";
}
我的疑问是,为什么在将文件写入Android Q中的getExternalFilesDir()子目录路径时抛出ENOENT错误?
如果该路径与getExternalFilesDir(null)不相关,则ENOENT有效
示例代码:
try {
assetsList = xAssetMgr.list(xFromDirectory);
for (String assetName : assetsList) {
String assetFullName = new File(xFromDirectory, assetName).toString();
if (!assetName.startsWith("images") && !assetName.startsWith("sounds") && !assetName.startsWith("webkit")) {
boolean isDirectory = 0 != xAssetMgr.list(assetFullName).length;
if (isDirectory) {
Log.v(_gDebugTag, "Asset " + assetFullName + " is directory");
File outDir = new File(xToDirectory, assetFullName);
outDir.mkdirs();
copyAssetsDirectory(xAssetMgr, assetFullName, xToDirectory);
outDir = null;
} // if
else {
// asset is a file
Log.v(_gDebugTag, "Asset " + assetFullName + " is file");
InputStream inStream = xAssetMgr.open(assetFullName);
File outFile = new File(xToDirectory, assetFullName);
FileOutputStream outStream = new FileOutputStream(outFile);
Log.i(_gDebugTag, "Copying " + outFile);
copyAsset(inStream, outStream);
inStream.close();
outStream.close();
outStream = null;
outFile = null;
} // else if
}
else
{
Log.v(_gDebugTag, "Skip Android entry " + assetName);
} // else
assetFullName = null;
} // for
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
示例日志文件: *
07-19 15:10:01.800 18275 18300 V胶水:调用copyAssetsDirectory 从到 /storage/emulated/0/Android/data/com.mp.test/files/com.mp.test 07-19 15:10:01.804 18275 18300 W System.err:java.io.FileNotFoundException: /storage/emulated/0/Android/data/com.mp.test/files/com.mp.test/0.key: 打开失败:ENOENT(没有此类文件或目录)07-19 15:10:01.804 18275 18300 W System.err:在 libcore.io.IoBridge.open(IoBridge.java:496)07-19 15:10:01.804 18275 18300 W System.err:在 java.io.FileOutputStream。(FileOutputStream.java:235)07-19 15:10:01.804 18275 18300 W System.err:在 java.io.FileOutputStream。(FileOutputStream.java:186)07-19 15:10:01.804 18275 18300 W System.err:在 com.mp.test.TestCorePak.copyAssetsDirectory(TestCorePak.java:94)07-19 15:10:01.804 18275 18300 W System.err:在 com.nagra.CppUnitForAndroid.TestWrapper.start(TestWrapper.java:96) 07-19 15:10:01.804 18275 18300 W System.err:at com.nagra.CppUnitForAndroid.TestWrapper.access $ 200(TestWrapper.java:58) 07-19 15:10:01.804 18275 18300 W System.err:at com.nagra.CppUnitForAndroid.TestWrapper $ WrapperThread.run(TestWrapper.java:443) 07-19 15:10:01.804 18275 18300 W System.err:由以下原因引起: android.system.ErrnoException:打开失败:ENOENT(无此文件或 目录)07-19 15:10:01.804 18275 18300 W System.err:在 libcore.io.Linux.open(本机方法)07-19 15:10:01.804 18275 18300 W System.err:位于libcore.io.ForwardingOs.open(ForwardingOs.java:167) 07-19 15:10:01.804 18275 18300 W System.err:at libcore.io.BlockGuardOs.open(BlockGuardOs.java:252)07-19 15:10:01.804 18275 18300 W System.err:在 libcore.io.ForwardingOs.open(ForwardingOs.java:167)07-19 15:10:01.804 18275 18300 W System.err:在 android.app.ActivityThread $ AndroidOs.open(ActivityThread.java:7242) 07-19 15:10:01.805 18275 18300 W System.err:在 libcore.io.IoBridge.open(IoBridge.java:482)07-19 15:10:01.805 18275 18300 W System.err:...还有6个
*