<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>RemoteDroid &#187; Java</title>
	<atom:link href="http://remotedroid.net/blog/category/java/feed/" rel="self" type="application/rss+xml" />
	<link>http://remotedroid.net/blog</link>
	<description>Use your Android phone as a wireless keyboard and mouse to remote control your computer</description>
	<lastBuildDate>Wed, 08 Sep 2010 00:52:37 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Android memory leaks</title>
		<link>http://remotedroid.net/blog/2010/08/13/android-memory-leaks/</link>
		<comments>http://remotedroid.net/blog/2010/08/13/android-memory-leaks/#comments</comments>
		<pubDate>Fri, 13 Aug 2010 09:34:15 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Android]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Tech]]></category>

		<guid isPermaLink="false">http://remotedroid.net/blog/?p=103</guid>
		<description><![CDATA[Since I&#8217;ve been banging my head against this for the last 12 hours, I thought I&#8217;d post a solution here, so other people might stumble across it and benefit.
I ran into two memory leaks in Android itself in WebView, and Typeface. Both are because of bugs in the OS, so if you&#8217;re encountering memory leaks, [...]]]></description>
			<content:encoded><![CDATA[<p>Since I&#8217;ve been banging my head against this for the last 12 hours, I thought I&#8217;d post a solution here, so other people might stumble across it and benefit.</p>
<p>I ran into two memory leaks in Android itself in <a href="http://developer.android.com/reference/android/webkit/WebView.html">WebView</a>, and <a href="http://developer.android.com/reference/android/graphics/Typeface.html">Typeface</a>. Both are because of bugs in the OS, so if you&#8217;re encountering memory leaks, and all your code looks pretty watertight, (Meaning you never pass Activities as a Context to a View, and you use WeakReferences for static inner classes) you might be hitting one of these.</p>
<p>The first bug for WebView is described <a href="http://code.google.com/p/android/issues/detail?id=5067">here.</a></p>
<p>The solution is just to call destroy() on your webview in the onDestroy() method of the containing activity, then set any references to that WebView to null, just in case. The app I&#8217;m working on is webview heavy, so this was causing some grief.</p>
<p>The second bug for Typeface is described <a href="http://code.google.com/p/android/issues/detail?id=9904">here.</a></p>
<p>The issue was that I was creating a new Typeface from assets in the onCreate method of my activity. Every time I did this, it was allocating around 700k of memory, which then never got released. Big, big problems. The solution in this case is to initialize the typeface object in a static class (You can subclass Application and stick it there if you want, but be aware that you can&#8217;t call getAssets() before the first activity gets it&#8217;s onCreate method called.) and then use that one instance for any TextViews you may have.</p>
<p>The only way I managed to figure out what was causing the leaks was to go into adb shell, and then use dumpsys meminfo. That showed me a whole lot of bad.</p>
]]></content:encoded>
			<wfw:commentRss>http://remotedroid.net/blog/2010/08/13/android-memory-leaks/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Loading images from jar files in windows</title>
		<link>http://remotedroid.net/blog/2009/01/05/31/</link>
		<comments>http://remotedroid.net/blog/2009/01/05/31/#comments</comments>
		<pubDate>Mon, 05 Jan 2009 21:30:33 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[Tech]]></category>

		<guid isPermaLink="false">http://remotedroid.net/blog/?p=31</guid>
		<description><![CDATA[In the first version of the server app, I was telling Windows users to start the program by clicking on a .bat file because for some reason, the thing wasn&#8217;t starting up when clicking on the .jar file directly.
I&#8217;d seen other apps that did start from the jar file just fine, and after a lot [...]]]></description>
			<content:encoded><![CDATA[<p>In the first version of the server app, I was telling Windows users to start the program by clicking on a .bat file because for some reason, the thing wasn&#8217;t starting up when clicking on the .jar file directly.</p>
<p>I&#8217;d seen other apps that did start from the jar file just fine, and after a lot of hunting around, I figured out why. When you search google for <a href="http://www.google.com/search?q=load+image+from+jar&#038;ie=utf-8&#038;oe=utf-8&#038;aq=t&#038;rls=org.mozilla:en-US:official&#038;client=firefox-a">load image from jar file</a>, most of the hits have you getting a URL object like this:</p>
<p>
<pre>
URL url = this.getClass().getResource("the_image.jpg");
</pre>
<!--start_raw-->

then using that to load the actual image.

This works just fine on OSX, and Linux, but does absolutely nothing on Windows for some reason. Instead, on Windows, you have to get a JarFile object for the jar you're loading from, then get a ZipEntry (JarEntry, whatever) from that, then get an InputStream from the ZipEntry, turn that into a BufferedInputStream, then pass that off to your default Toolkit to turn into an image.

Of course this means you have to figure out whether the app's being run in a jar file in the first place or not. This is how I'm doing it:


<!--start_raw-->
<pre>
URL fileURL = this.getClass().getProtectionDomain().getCodeSource().getLocation();
String sBase = fileURL.toString();
if ("jar".equals(sBase.substring(sBase.length()-3, sBase.length()))) {
&#09;MyClass.jar = new JarFile(new File(fileURL.toURI()));
}
</pre>
<!--end_raw-->


after that, you can load the image ike this:


<!--start_raw-->
<pre>
Image imReturn = null;
try {
&#09;if (jar == null) {
&#09;&#09;// This is used if you're not loading from a jar
&#09;&#09;imReturn = this.toolkit.createImage(this.getClass().getClassLoader().getResource(sImage));
&#09;} else {
&#09;&#09;//
&#09;&#09;BufferedInputStream bis = new BufferedInputStream(jar.getInputStream(jar.getEntry(sImage)));
&#09;&#09;ByteArrayOutputStream buffer=new ByteArrayOutputStream(4096);
&#09;&#09;int b;
&#09;&#09;while((b=bis.read())!=-1) {
&#09;&#09;&#09;buffer.write(b);
&#09;&#09;}
&#09;&#09;byte[] imageBuffer=buffer.toByteArray();
&#09;&#09;imReturn = this.toolkit.createImage(imageBuffer);
&#09;&#09;bis.close();
&#09;&#09;buffer.close();
&#09;}
} catch (IOException ex) {

}
return imReturn;
</pre>
</p>
]]></content:encoded>
			<wfw:commentRss>http://remotedroid.net/blog/2009/01/05/31/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>
