copy assets

This commit is contained in:
badaix 2016-01-03 13:15:38 +01:00
parent baaf8d980d
commit c53a60d492
3 changed files with 61 additions and 1 deletions

View file

@ -10,4 +10,4 @@ public class ApplicationTest extends ApplicationTestCase<Application> {
public ApplicationTest() {
super(Application.class);
}
}
}

View file

@ -2,6 +2,7 @@ package de.badaix.snapcast;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.nsd.NsdManager;
import android.net.nsd.NsdServiceInfo;
import android.support.v7.app.ActionBarActivity;
@ -15,6 +16,12 @@ import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
private static final String TAG = "Main";
@ -51,9 +58,62 @@ public class MainActivity extends ActionBarActivity implements View.OnClickListe
buttonScan.setOnClickListener(this);
buttonStart.setOnClickListener(this);
buttonStop.setOnClickListener(this);
copyAssets();
initializeDiscoveryListener();
}
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);
}
if (files != null) for (String filename : files) {
InputStream in = null;
OutputStream out = null;
try {
File outFile = new File(getFilesDir(), filename);
// if (outFile.exists() && (outFile.length() == assetManager.openFd(filename).getLength())) {
// Log.d("Main", "Exists: " + outFile.getAbsolutePath());
// continue;
// }
Log.d("Main", "Asset: " + outFile.getAbsolutePath());
in = assetManager.open(filename);
out = new FileOutputStream(outFile);
copyFile(in, out);
Runtime.getRuntime().exec("chmod 755 " + outFile.getAbsolutePath()).waitFor();
} catch (Exception e) {
Log.e("tag", "Failed to copy asset file: " + filename, e);
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
// NOOP
}
}
if (out != null) {
try {
out.close();
} catch (IOException e) {
// NOOP
}
}
}
}
}
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);
}
}
private void start() {
Intent i = new Intent(this, SnapclientService.class);