Reputation: 1
How to copy my zip file (I have this file in my res files) to internal storage (path: data/data/[myPackageName]/files) ?
I tried to do so, but it only worked once and not anymore.
FileInputStream in = null;
InputStreamReader reader = null;
try {
char[] inputBuffer = new char[256];
in = openFileInput("file.zip");
reader = new InputStreamReader(in);
reader.read(inputBuffer);
String myFile = new String(inputBuffer);
} catch (Exception e) {;}
finally {
try {
if (reader != null)reader.close();
} catch (IOException e) {; }
try {
if (in != null)in.close();
} catch (IOException e) {;}
}
Upvotes: 0
Views: 386
Reputation: 1
Firstly, create folder assets along this path: .../com.example.yourappname/src/main/assets and place yourFileName there
Secondly use these methods:
String dirPath = this.getFilesDir().getAbsolutePath() + "/yourCustomFolder/";
File dir = new File(dirPath);
if (!dir.exists()) {
dir.mkdirs();
}
AssetManager assetManager = getAssets();
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
File outFile = new File(dirPath, filename);
out = new FileOutputStream(outFile);
copyFile(in, out);
Toast.makeText(this, "Saved!", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
Toast.makeText(this, "Failed!", Toast.LENGTH_SHORT).show();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1){
out.write(buffer, 0, read);
}
}```
-------------------------------------------------------------------------------
After all, use this to call a general method: copyAssets("yourFileName");
Upvotes: 0