我需要将文件从资产复制到外部存储。这是我的代码:
File f = new File(Environment.getExternalStorageDirectory() + File.separator
+ "MyApp" + File.separator + "tessdata" + File.separator + "eng.traineddata");
if (!f.exists()) {
AssetManager assetManager = getAssets();
try {
f.createNewFile();
InputStream in = assetManager.open("eng.traineddata");
OutputStream out = new FileOutputStream(Environment.getExternalStorageDirectory() + File.separator
+ "MyApp" + File.separator + "tessdata" + File.separator + "eng.traineddata");
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1)
out.write(buffer, 0, read);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (IOException e) {
Log.e("tag", "Failed to copy asset file: ", e);
}
}
我还在清单中添加了权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
执行时发生异常f.createNewFile()
,说“权限被拒绝”。
请问我该如何解决?