Saturday, August 27, 2011
Copying files in Java
This is something I've never really done much of, but when I've seen snippets of code which do this I've always thought how ugly and cumbersome it looks:
File srcFile = new File(sourceFilePath);
File destFile = new File(destinationFilePath);
FileInputStream in = new FileInputStream(srcFile);
FileOutputStream out = new FileOutputStream(destFile);
byte[] buf = new byte[2048];
int len;
while ((len = in.read(buf)) > 0)
{
try
{
out.write(buf, 0, len);
}
catch (Exception e)
{
}
}
out.flush();
in.close();
out.close();
etc...
But there is now, at last, an easier way:
import java.nio.channels.FileChannel;
...
File srcFile = new File(sourceFilePath);
File destFile = new File(destinationFilePath);
FileInputStream in = new FileInputStream(srcFile);
FileOutputStream out = new FileOutputStream(destFile);
FileChannel srcFileChannel = in.getChannel();
FileChannel destFileChannel = out.getChannel();
destFileChannel.transferFrom(srcFileChannel, 0, srcFileChannel.size());
srcFileChannel.close();
destFileChannel.close();
...
Sunday, August 7, 2011
Building Android with Ant
To create a build using an ant build.xml script, you'll need a local.properties file. This point to your sdk installation directory.
You can create this with the android tool like so:
android update project --path .
Execute from your project's directory. The full stop is important!
Remember you'll need to add keystore and proguard if you want to sign/obfustcate etc:
key.store=/path_to_keystore/keystore
key.alias=key_store_alias
proguard.dir=/path_to_proguard/lib (seems to use lib dir rather than bin)
(See here for more about signing and obfuscation)
You can create this with the android tool like so:
android update project --path .
Execute from your project's directory. The full stop is important!
Remember you'll need to add keystore and proguard if you want to sign/obfustcate etc:
key.store=/path_to_keystore/keystore
key.alias=key_store_alias
proguard.dir=/path_to_proguard/lib (seems to use lib dir rather than bin)
(See here for more about signing and obfuscation)
Saturday, July 23, 2011
Debugging with Nunit and Visual Studio Express
It's possible to set breakpoints in unit tests in the freebie versions of MS Visual Studio:
- add a reference to the C:\Program Files\NUnit 2.5.10\bin\net-2.0\lib\nunit-console-runner.dll to your test project
- add the following class to your test project using System;
- in the test project properties, under 'Application' set the output type as 'Windows Application and the Startup objects as the class created in the previous step. Under the debug tab, browse to the folder containing the csproj for the test project in the 'Working directory' field and put the name of the csproj file in the C'Command line arguments' field
- set the test project as the startup project
namespace MyTests
{
class NUnitDebugRunner
{
[STAThread]
static void Main(string[] args)
{
NUnit.ConsoleRunner.Runner.Main(args);
}
}
}
Now when you hit F5, any breakpoints in the tests will be hit.
Thanks to Blokley
Wednesday, July 20, 2011
Executing a script at startup
Having just reinstalled Linux Mint, I needed to install and setup Twonky media server again. Installing was easy with the supplied script, but to start the server (and kill it on shutdown - don't know if this is strictly necessary) you have to create a symbolic link in /etc/rc2.d which points to the twonky script in /etc/init.d and give it a name starting with Snn, where nn is a 2 digit number from 01 to 99:
sudo ln -s /etc/init.d/twonkyserver /etc/rc2.dS99twonky
To kill the server, do the same in /etc/rc0.d but call the symbolic link Knn:
sudo ln -s /etc/init.d/twonkyserver /etc/rc0.d/K99twonky
sudo ln -s /etc/init.d/twonkyserver /etc/rc2.dS99twonky
To kill the server, do the same in /etc/rc0.d but call the symbolic link Knn:
sudo ln -s /etc/init.d/twonkyserver /etc/rc0.d/K99twonky
Monday, July 18, 2011
Dual monitor setup on Linux Mint
I had a dual monitor setup running perfectly on Linux Mint 10, but struggled to get it going under Mint 11. From Googling it appears I was not alone, so I decided to skip 11 and revert to 10.
These are the steps for a clean Mint 10 installation:
These are the steps for a clean Mint 10 installation:
- open the additional drivers application and install the recommended drivers and restart the machine
- open the NVIDIA settings and enable the second monitor and check the Xinemara box
- restart the machine
That's it!
Sunday, July 3, 2011
Forcing screen to stay on
Here's a nice way to make the screen stay on for a particular activity. Just put this in the onCreate method:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
It's nicer than the POWERLOCK method, which requires an additional permission in the manifest:
<uses-permission android:name="android.permission.WAKE_LOCK">
And in your Activity:
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
this.mWakeLock.acquire();
//and when you're done with the WakeLock:
this.mWakeLock.release();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
It's nicer than the POWERLOCK method, which requires an additional permission in the manifest:
<uses-permission android:name="android.permission.WAKE_LOCK">
And in your Activity:
final PowerManager pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
this.mWakeLock = pm.newWakeLock(PowerManager.SCREEN_DIM_WAKE_LOCK, "My Tag");
this.mWakeLock.acquire();
//and when you're done with the WakeLock:
this.mWakeLock.release();
Monday, May 30, 2011
Android strings.xml
Here's a strange one - I was working on an Android application, using the strings.xml file for the apps string resources. It so happened that I needed to have two strings in both underlined and non-underlined forms. So I added:
<string name="average_underlined"><u>average</u></string>
<string name="recent_underlined"><u>recent*</u></string>
<string name="average">average</string>
<string name="recent">recent</string>
to the strings.xml file. However, when I ran the app the underlined versions appeared where I expected the regular version to appear. After checking and rechecking I was happy the code was correct. Bizarrely, the fix was to have the non-underlined versions declared first:
<string name="average">average</string>
<string name="recent">recent*</string>
<string name="average_underlined"><u>average</u></string>
<string name="recent_underlined"><u>recent*</u></string>
If anyone can explain this to me please do get in touch!
<string name="average_underlined"><u>average</u></string>
<string name="recent_underlined"><u>recent*</u></string>
<string name="average">average</string>
<string name="recent">recent</string>
to the strings.xml file. However, when I ran the app the underlined versions appeared where I expected the regular version to appear. After checking and rechecking I was happy the code was correct. Bizarrely, the fix was to have the non-underlined versions declared first:
<string name="average">average</string>
<string name="recent">recent*</string>
<string name="average_underlined"><u>average</u></string>
<string name="recent_underlined"><u>recent*</u></string>
If anyone can explain this to me please do get in touch!
Subscribe to:
Posts (Atom)