Added comments and summaries to CopyAssets.java

This commit is contained in:
Chris 2019-11-12 18:47:17 -05:00
parent f52408abff
commit a2b9823bbf
1 changed files with 27 additions and 4 deletions

View File

@ -11,32 +11,49 @@ import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
/**
* Allows assets from the apk to be copied to a local folder to be worked with
*/
public class CopyAssets { public class CopyAssets {
//TAG for debugging purposes
private final static String TAG = "CopyAssets"; private final static String TAG = "CopyAssets";
//MainActivity context
private final Context context; private final Context context;
/**
* Constructor that sets the context
* @param appContext = The activities context
*/
public CopyAssets(Context appContext) public CopyAssets(Context appContext)
{ {
this.context = appContext; this.context = appContext;
} }
//Copies the needed files from the APK to a local directory so they can be used /**
* Copies the needed files from the APK to a local directory so they can be used
* @param asset = The asset to copy
* @param dir = The directory to copy the asset to
*/
public void copyAssets(String asset, String dir) { public void copyAssets(String asset, String dir) {
AssetManager assetManager = context.getAssets(); AssetManager assetManager = context.getAssets();
String[] files = null; String[] files = null;
File dest = new File(Environment.getExternalStorageDirectory() + dir); File dest = new File(Environment.getExternalStorageDirectory() + dir);
if(!dest.exists())
{ //Check is the directory exists
if(!dest.exists()) {
dest.mkdir();//directory is created; dest.mkdir();//directory is created;
} }
//Try to get the list of assets
try { try {
files = assetManager.list(asset); files = assetManager.list(asset);
} catch (IOException e) { } catch (IOException e) {
Log.e(TAG, "Failed to get asset file list.", e); Log.e(TAG, "Failed to get asset file list.", e);
} }
Log.e(TAG, "File Length: " + files.length);
//Open the asset and copy it
if (files != null) for (String filename : files) { if (files != null) for (String filename : files) {
InputStream in = null; InputStream in = null;
OutputStream out = null; OutputStream out = null;
@ -67,6 +84,12 @@ public class CopyAssets {
} }
} }
/**
* Short method that copies the files using an InputStream and OutputStream
* @param in = The input stream
* @param out = The output stream
* @throws IOException if one of the streams fail
*/
private void copyFile(InputStream in, OutputStream out) throws IOException { private void copyFile(InputStream in, OutputStream out) throws IOException {
byte[] buffer = new byte[1024]; byte[] buffer = new byte[1024];
int read; int read;