I need to copy a file from the assets to the external storage. Here is my code:
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);
}
}
I've also added the permission in the manifet
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
The exception occurred when is executed f.createNewFile()
, saying "Permission denied".
How can I fix it please?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…