java: copy file from assets to destination required by an Game

This commit is contained in:
rafal1137 2020-05-21 01:16:17 +02:00
parent 563dd269e8
commit d5ad6dc558

View file

@ -21,8 +21,10 @@ import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.FileAsyncHttpResponseHandler; import com.loopj.android.http.FileAsyncHttpResponseHandler;
import java.io.File; import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream;
import cz.msebera.android.httpclient.Header; import cz.msebera.android.httpclient.Header;
@ -48,6 +50,42 @@ public class ETLMain extends Activity {
return etl_drawable; return etl_drawable;
} }
/**
* Copy InputStream to a File.
* @param in inputstream to be saved
* @param file location of file to save inputstream in
*/
private void copyInputStreamToFile(InputStream in, File file) {
OutputStream out = null;
try {
out = new FileOutputStream(file);
byte[] buf = new byte[1024];
int len;
while((len=in.read(buf))>0){
out.write(buf,0,len);
}
}
catch (Exception e) {
e.printStackTrace();
}
finally {
// Ensure that the InputStreams are closed even if there's an exception.
try {
if ( out != null ) {
out.close();
}
// If you want to close the "in" InputStream yourself then remove this
// from here but ensure that you close it yourself eventually.
in.close();
}
catch ( IOException e ) {
e.printStackTrace();
}
}
}
/** /**
* Convert pixel metrics to dp * Convert pixel metrics to dp
* @param px value of px to be converted * @param px value of px to be converted
@ -87,10 +125,25 @@ public class ETLMain extends Activity {
etl_Layout.addView(textView, etl_Params_download); etl_Layout.addView(textView, etl_Params_download);
setContentView(etl_Layout); setContentView(etl_Layout);
File etl_pak = new File(getExternalFilesDir(null), "/etlegacy/etmain/pak0.pk3"); File etl_etlegacy = new File(getExternalFilesDir(null), "etlegacy/legacy/legacy_v2.76.pk3");
if (!etl_etlegacy.exists()) {
AssetManager assManager = getApplicationContext().getAssets();
InputStream is = null;
try {
is = assManager.open("legacy_v2.76.pk3");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
copyInputStreamToFile(is, etl_etlegacy);
}
File etl_pak0 = new File(getExternalFilesDir(null), "/etlegacy/etmain/pak0.pk3");
final Intent intent = new Intent(ETLMain.this, ETLActivity.class); final Intent intent = new Intent(ETLMain.this, ETLActivity.class);
if (etl_pak.exists()) { if (etl_pak0.exists()) {
startActivity(intent); startActivity(intent);
finish(); finish();
} }