<?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>nariyu&#039;s &#187; ActionScript</title>
	<atom:link href="http://blog.nariyu.jp/tag/actionscript/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.nariyu.jp</link>
	<description>memo-cho</description>
	<lastBuildDate>Fri, 03 Feb 2012 06:41:04 +0000</lastBuildDate>
	<language>ja</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>[AS3][AIR] AIR for Android の nativePath の取得</title>
		<link>http://blog.nariyu.jp/2012/01/get-nativepath-from-air-for-android/</link>
		<comments>http://blog.nariyu.jp/2012/01/get-nativepath-from-air-for-android/#comments</comments>
		<pubDate>Mon, 30 Jan 2012 19:41:52 +0000</pubDate>
		<dc:creator>nariyu</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[ActionScript]]></category>
		<category><![CDATA[Adobe AIR]]></category>
		<category><![CDATA[Android]]></category>

		<guid isPermaLink="false">http://blog.nariyu.jp/?p=17</guid>
		<description><![CDATA[ActionScript 3.0 と Adobe AIR のお話しです。 ハマりどころなのでメモしておきますね。 AIR for Android でネイティブパスを取得する場合、 File.applicationStor [...]]]></description>
			<content:encoded><![CDATA[<p>ActionScript 3.0 と Adobe AIR のお話しです。<br />
ハマりどころなのでメモしておきますね。</p>
<p>AIR for Android でネイティブパスを取得する場合、 <code><strong>File.applicationStorageDirectory</strong></code> （プライベート記憶領域）と、 <code><strong>File.applicationDirectory</strong></code> （アプリケーションディレクトリ）で取得する方法を変える必要があるよってお話しです。</p>
<p><span id="more-17"></span></p>
<p>※ AIR 3.1 のお話しです。</p>
<h3>以下ちょっと長いので読むのが面倒な人は、、</h3>
<p>これだけ覚えておきましょう。</p>
<p>AIR for Android は アプリケーションディレクトリ（<code>File#applicationDirectory</code>）のファイルは「<code>/android_asset/ほにゃらら</code>」で、プライベート記憶領域（<code>File#applicationStorageDirectory</code>）のファイルは「<code>File.applicationStorageDirectory.resolvePath(ほにゃらら).nativePath</code>」になります。</p>
<p>URL にする場合は、それぞれ「<code>file:///android_asset/ほにゃらら</code></code>」と「<code>new File(File.applicationStorageDirectory.resolvePath(ほにゃらら).nativePath).url</code>」で取得できます。</p>
<p>以下は原因とかちょっと詳しい解説です。特に必要なければ読まなくてOK。</p>
<h3>File.applicationStorageDirectory （プライベート記憶領域）</h3>
<p><code>File.applicationStorageDirectory</code> のファイルは nativePath プロパティの値でネイティブパスが取得できます。</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>File<span style="color: #000066; font-weight: bold;">.</span>applicationStorageDirectory<span style="color: #000066; font-weight: bold;">.</span>resolvePath<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;example.html&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">.</span>nativePath<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>上のコードでは Android のプライベート記憶領域である「<code>/data/data/air.[アプリケーションID].debug/[アプリケーションID].debug/Local Store/example.html</code>」が取得できます。（デバッグ実行の場合）</p>
<h3>File.applicationDirectory （アプリケーションディレクトリ）</h3>
<p><code>File.applicationDirectory</code> の場合、なんと Android では <strong><code>nativePath</code> プロパティの値が正しく取得できません</strong>。</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>File<span style="color: #000066; font-weight: bold;">.</span>applicationDirectory<span style="color: #000066; font-weight: bold;">.</span>resolvePath<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;example.html&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">.</span>nativePath<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>上のコードは ""（空文字）になってしまいます。（<code>null</code> ではないです。）</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #004993;">trace</span><span style="color: #000000;">&#40;</span>File<span style="color: #000066; font-weight: bold;">.</span>applicationDirectory<span style="color: #000066; font-weight: bold;">.</span>nativePath<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>も同じです。<br />
※ AIR for iOS の場合は正しく取得できます。</p>
<ul>
<li>File.applicationDirectory.nativePath</li>
<li>File.applicationDirectory.resolvePath("example.html").nativePath</li>
</ul>
<p>どちらもKARA!</p>
<p>公式のドキュメントを見てもどこにも書いてないです。書いてあるかもしれないけど見つけられなかったです。</p>
<p>どうやら、<code>File.applicationDirectory</code> の場合のみ、<code>nativePath</code> プロパティの値を正しく取得することができないようです。<br />
エラーにもならないのでハマります。</p>
<p>※ <code>.resolvePath("example.html")</code> までは正常に処理されます。</p>
<h3>URL への変換</h3>
<p><code>StageWebView#loadURL(url:String)</code> にローカルの HTML ファイルを読み込ませるようなとき、<code><strong>File.applicationDirectory</strong></code> の場合は、</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">url</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;file:///android_asset/&quot;</span> <span style="color: #000066; font-weight: bold;">+</span> <span style="color: #990000;">&quot;example.html&quot;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>として、</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #990000;">&quot;file:///android_asset/example.html&quot;</span></pre></div></div>

<p>のような URL を作成します。</p>
<p><code><strong>File.applicationStorageDirectory</strong></code> の場合は、</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">url</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #0033ff; font-weight: bold;">new</span> File<span style="color: #000000;">&#40;</span>File<span style="color: #000066; font-weight: bold;">.</span>applicationStorageDirectory<span style="color: #000066; font-weight: bold;">.</span>resolvePath<span style="color: #000000;">&#40;</span><span style="color: #990000;">&quot;example.html&quot;</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">.</span>nativePath<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">url</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<p>とすることで</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #990000;">&quot;file:///data/data/air.[アプリケーションID].debug/[アプリケーションID].debug/Local Store/example.html&quot;</span></pre></div></div>

<p>のような URL が作成できます。これを StageWebView 等に読み込ませれば OK。</p>
<h3>まとめ</h3>
<p>まとめると次のようなコードになります。</p>

<div class="wp_syntax"><div class="code"><pre class="actionscript3" style="font-family:monospace;"><span style="color: #009900; font-style: italic;">// ↓ このファイルを取得したい！</span>
<span style="color: #6699cc; font-weight: bold;">var</span> fileName<span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span> = <span style="color: #990000;">&quot;example.html&quot;</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">// 取得する URL 文字列</span>
<span style="color: #6699cc; font-weight: bold;">var</span> <span style="color: #004993;">url</span><span style="color: #000066; font-weight: bold;">:</span><span style="color: #004993;">String</span><span style="color: #000066; font-weight: bold;">;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">// File.applicationStorageDirectory の中のファイルなら...</span>
<span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span><span style="color: #000000;">&#91;</span>applicationStoregeDirectory の場合<span style="color: #000000;">&#93;</span><span style="color: #000000;">&#41;</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #004993;">url</span> = <span style="color: #0033ff; font-weight: bold;">new</span> File<span style="color: #000000;">&#40;</span>File<span style="color: #000066; font-weight: bold;">.</span>applicationStorageDirectory<span style="color: #000066; font-weight: bold;">.</span>resolvePath<span style="color: #000000;">&#40;</span>fileName<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">.</span>nativePath<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">url</span><span style="color: #000066; font-weight: bold;">;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">// File.applicationDirectory の中のファイルなら...</span>
<span style="color: #0033ff; font-weight: bold;">else</span>
<span style="color: #000000;">&#123;</span>
    <span style="color: #009900; font-style: italic;">// Android 端末なら...</span>
    <span style="color: #0033ff; font-weight: bold;">if</span> <span style="color: #000000;">&#40;</span>isAndroid<span style="color: #000000;">&#41;</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #004993;">url</span> = <span style="color: #990000;">&quot;file:///android_asset/&quot;</span> <span style="color: #000066; font-weight: bold;">+</span> fileName<span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #000000;">&#125;</span>
&nbsp;
    <span style="color: #009900; font-style: italic;">// Android 端末以外なら...</span>
    <span style="color: #0033ff; font-weight: bold;">else</span>
    <span style="color: #000000;">&#123;</span>
        <span style="color: #004993;">url</span> = <span style="color: #0033ff; font-weight: bold;">new</span> File<span style="color: #000000;">&#40;</span>File<span style="color: #000066; font-weight: bold;">.</span>applicationDirectory<span style="color: #000066; font-weight: bold;">.</span>resolvePath<span style="color: #000000;">&#40;</span>fileName<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">.</span>nativePath<span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">url</span><span style="color: #000066; font-weight: bold;">;</span>
    <span style="color: #000000;">&#125;</span>
<span style="color: #000000;">&#125;</span>
&nbsp;
<span style="color: #009900; font-style: italic;">// StageWebView に読み込ませてしまえ</span>
<span style="color: #6699cc; font-weight: bold;">var</span> webView<span style="color: #000066; font-weight: bold;">:</span>StageWebView = <span style="color: #0033ff; font-weight: bold;">new</span> StageWebView<span style="color: #000066; font-weight: bold;">;</span>
webView<span style="color: #000066; font-weight: bold;">.</span><span style="color: #004993;">stage</span> = <span style="color: #004993;">stage</span><span style="color: #000066; font-weight: bold;">;</span>
webView<span style="color: #000066; font-weight: bold;">.</span>viewport = <span style="color: #004993;">rect</span><span style="color: #000066; font-weight: bold;">;</span>
webView<span style="color: #000066; font-weight: bold;">.</span>loadURL<span style="color: #000000;">&#40;</span><span style="color: #004993;">url</span><span style="color: #000000;">&#41;</span><span style="color: #000066; font-weight: bold;">;</span></pre></div></div>

<h3>もうひとつまとめ</h3>
<p>表にするとこんな感じ。（見辛い）</p>
<h4>File.applicationDirectory</h4>
<div class="table-wrapper">
<table summary="AIR for Android の File まとめ">
<tr>
<th>コード</th>
<th>結果</th>
</tr>
<tr>
<td>File.applicationDirectory.url</td>
<td>app:/</td>
</tr>
<tr>
<td>File.applicationDirectory.nativePath</td>
<td>""（空文字）</td>
</tr>
<tr>
<td>File.applicationDirectory.resolvePath("example.html").url</td>
<td>app:/example.html</td>
</tr>
<tr>
<td>File.applicationDirectory.resolvePath("example.html").nativePath</td>
<td>""（空文字）</td>
</tr>
<tr>
<td>"file:///android_asset/example.html"</td>
<td>file:///android_asset/example.html</td>
</tr>
</table>
</div>
<h4>File.applicationStorageDirectory</h4>
<div class="table-wrapper">
<table summary="AIR for Android の File まとめ">
<tr>
<th>コード</th>
<th>結果</th>
</tr>
<tr>
<td>File.applicationStorageDirectory.url</td>
<td>app-storage:/</td>
</tr>
<tr>
<td>File.applicationStorageDirectory.nativePath</td>
<td>/data/data/air.[アプリケーションID].debug/[アプリケーションID].debug/Local Store</td>
</tr>
<tr>
<td>File.applicationStorageDirectory.resolvePath("example.html").url</td>
<td>app-storage:/example.html</td>
</tr>
<tr>
<td>File.applicationStorageDirectory.resolvePath("example.html").nativePath</td>
<td>/data/data/air.[アプリケーションID].debug/[アプリケーションID].debug/Local Store/example.html</td>
</tr>
<tr>
<td>new File(File.applicationStorageDirectory.resolvePath("example.html").nativePath).url</td>
<td>file:///data/data/air.[アプリケーションID].debug/[アプリケーションID].debug/Local%20Store/example.html</td>
</tr>
</table>
</div>
<p>さて、なんでこんな面倒なことになってんでしょう？</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.nariyu.jp/2012/01/get-nativepath-from-air-for-android/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

