Issue
So basically I'm working on GFX tool for pubg mobile which copy file from app (assets) to another location (internal storage)
Main: So the problem is I'm new in android development as well as in kotlin and java too and i dont know how can i copy file from app (assets) to another location (internal storage) when i click button on my app so please if anyone know please help it's really appreciate
I am writing an Android application and I need to copy 2 files into the device's internal storage. Currently I am able to run my application after manually copying these two files under the device's "/data/data/[my_package_name]/files" folder using DDMS. I need to put these two files into that folder(or any folder) while my application is being installed on the device. The tip here says files within the "res/raw/" directory will be deployed to the device and will be accessible through openRawResource()
, but it doesn't tell where these files will be put inside the internal memory. To summerize, I have two files in my project folder (assets, raw, any other folder) and I need to copy these two inside a path of internal memory. How can I achieve this? Do I need any permissions included to my AndroidManifest.xml?
Thank you in advance
Edit : Answer Found
So basically it will copy all the data from Asset folder to Internal Storage (Download Folder of Android), Call the copyAsset() function in onCreate.
private void copyAssets() {
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("");
} catch (IOException e) {
Log.e("tag", "Failed to get asset file list.", e);
}
for (String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
String outDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download/";
File outFile = new File(outDir, filename);
out = new FileOutputStream(outFile);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, e);
}
}
}
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);
}
}
Solution
So basically it will copy all the data from Asset folder to Internal Storage (Download Folder of Android), Call the copyAsset() function in onCreate.
private void copyAssets() {
AssetManager assetManager = getAssets();
String[] files = null;
try {
files = assetManager.list("");
} catch (IOException e) {
Log.e("tag", "Failed to get asset file list.", e);
}
for (String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
in = assetManager.open(filename);
String outDir = Environment.getExternalStorageDirectory().getAbsolutePath() + "/Download/";
File outFile = new File(outDir, filename);
out = new FileOutputStream(outFile);
copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (IOException e) {
Log.e("tag", "Failed to copy asset file: " + filename, e);
}
}
}
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);
}
}
Answered By - Akshat Bhuhagal
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.