2013年3月29日金曜日

YouTubeInitializationResult

YouTubeInitializationResult

YouTubeInitializationResultは、onInitializationFailureメソッドの第二引数として添付されてくる。
インストールされているYouTubeアプリのバージョンが古い場合に有益な振る舞いをしてくれる。
getErrorDialogメソッドには、引数にDialogInterface.OnCancelListenerが存在しない版もある。この版の場合、setOnCancelListenerメソッドでリスナを追加するのであろうか。
(めったにいないであろう、私のような)利用者が「戻る」キーを押し下げることも有りうるのであるから、OnCancelListenerの実装は必須では無いだろうか。

YouTubeアプリのバージョンが古い場合、「YouTubeアプリを更新」画面が表示される。そして「YouTubeアプリを更新」ボタンを押し下げると、onSaveInstanceStateメソッドが呼び出される。このタイミングで呼び出されるのであるから、onSaveInstanceStateメソッド内に記載する各種オブジェクトの初期化状況について注意を払い、NullPointerExceptionが発生しないようにする必要がある。

「YouTubeアプリを更新」ボタンの押し下げ後、画面はGoogle PlayのYouTubeアプリ更新画面になる。この画面で「戻る」キーを押し下げると、onActivityResultメソッドが呼び出される。この場合、このメソッドの第2引数はRESULT_OKではない。

Google PlayのYouTubeアプリ更新画面で、適切に更新を実行した後、onActivityResultメソッド内で、YouTubePlayerView#initializeメソッドを実行するように仕込んでいたが、原因不明のエラーが発生してしまった。YouTubePlayerView#initializeメソッドとは別の要因のエラーかもしれない。もはや当方ではYouTubeアプリの更新実験はできない。

static final int ID_Error_requestCode_YouTubeAppDialog = 24;
private YouTubePlayerView ytView;
Dialog DlgYouTubeAPI;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ytView = (YouTubePlayerView)findViewById(R.id.youtube_view);
    ytView.initialize(DEVELOPER_KEY, this);
    (以下省略)

@Override
public void onInitializationFailure(Provider arg0, YouTubeInitializationResult arg1){
    if(arg1.isUserRecoverableError()==true){
        //インストールされているYouTubeアプリのバージョンが古い場合
        DlgYouTubeAPI = arg1.getErrorDialog(this, ID_Error_requestCode_YouTubeAppDialog, this);
        DlgYouTubeAPI.show();//新版のインストールを促すダイアログが表示される。
    }
    else{//Recoverableでは無いエラーも存在しえる。
    (以下省略)

@Override
public void onCancel(DialogInterface arg0){
    if(arg0==DlgYouTubeAPI){//新版のインストールを促すダイアログ表示時に、「戻る」キーを押し下げた場合、ここに来る。
        finish();
        return;
    }(以下省略)

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data){
    switch(requestCode){
    case ID_Error_requestCode_YouTubeAppDialog:
        if(resultCode==RESULT_OK){
            //ここに何を書けば良いのだ?
            finish();//取り敢えず、再起動をしていただこうかね?
        }
        else{//Google PlayのYouTubeアプリ更新画面で「戻る」キーの押し下げ。
            finish();
        }
        break;
    }
    super.onActivityResult(requestCode, resultCode, data);
}

2013年3月3日日曜日

has leaked ServiceConnection com.google.android.youtube.player.internal.r

has leaked ServiceConnection com.google.android.youtube.player.internal.r

インターネットに接続しない環境下で、YouTubePlayerView.initialize()を実行すると、onInitializationFailureメソッドで結果を受け取ることになる。そして「戻る」キーを押し下げてアプリを終了させると下記のエラーが出る。むむむ!
E/ActivityThread(7188): Activity jp.Androyer.xxx.MainActivity has leaked ServiceConnection com.google.android.youtube.player.internal.r$e@41babb20 that was originally bound here
E/ActivityThread(7188): android.app.ServiceConnectionLeaked: Activity jp.Androyer.xxx.MainActivity has leaked ServiceConnection com.google.android.youtube.player.internal.r$e@41babb20 that was originally bound here
Leak while switching activity with YouTubeThumbnailViewsに似たようなエラーがあるが、回答はonInitializationSuccessメソッドで結果を受け取ることを前提にしている。
この回答から察するに、おそらく、エラーになったにも関わらず、YouTubePlayerを生成してしまい、かつYouTubePlayerをrelease()していないからだと思われます。

インターネットへの接続状態判断であれば、別の方法でできるので問題はない。しかし、他の原因でエラーが発生した場合、どうなるのか不安になる。

2013年2月23日土曜日

実験:getDrawingCache()により取得するBitmapには子Viewも含まれるのか

実験:getDrawingCache()により取得するBitmapには子Viewも含まれるのか

結論:含まれる

親View{
 子View
 子View
 ...


上記関係にあるViewにおいて、親ViewでgetDrawingCache()して取得したBitmapには子Viewの画像も描かれている。
実験に使ったコードは下記のとおり。

public class MainActivity extends Activity
    implements
    OnClickListener
    {
    Button btn1, btn2;
    Bitmap bm;
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn1 = (Button)findViewById(R.id.Button1);
        btn1.setOnClickListener(this);
        btn2 = (Button)findViewById(R.id.Button2);
        btn2.setOnClickListener(this);
        bm = null;
    }

    @Override
    protected void onDestroy() {
        if(bm!=null) bm.recycle();
        super.onDestroy();
    }

    @Override
    public void onClick(View v) {
        if(bm!=null){
            bm.recycle();
            bm = null;
        }
     
        if(v==btn1) v = (RelativeLayout)findViewById(R.id.RelativeLayout);
        else{
            if(v==btn2) v = (TextView)findViewById(R.id.TextView);
            else v = null;
        }
        if(v!=null){
            v.setDrawingCacheEnabled(true);
            bm = v.getDrawingCache().copy(Bitmap.Config.ARGB_8888, false);
            v.setDrawingCacheEnabled(false);
            ImageView iv = (ImageView)findViewById(R.id.Image2);
            iv.setImageBitmap(bm);
        }
    }
}

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    android:orientation="vertical"
    >
    <RelativeLayout
        android:id="@+id/RelativeLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#9999ff"
        >
        <ImageView
            android:id="@+id/Image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/ic_launcher"
            android:contentDescription="@string/app_name"
            android:background="#99ff99"
            >
        </ImageView>
        <Button
            android:id="@+id/Button1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/Image"
            android:text="@string/Button1" 
            >
        </Button>
        <TextView
            android:id="@+id/TextView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_toRightOf="@id/Image"
            android:layout_below="@id/Button1"
            android:gravity="center"
            android:text="@string/hello_world" 
            android:background="#ff9999"
        />
    </RelativeLayout>
    <Button
        android:id="@+id/Button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/Button2" 
        >
    </Button>
    <ImageView
        android:id="@+id/Image2"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:src="@drawable/ic_launcher"
        android:contentDescription="@string/app_name"
        >
    </ImageView>
</LinearLayout>


2013年2月16日土曜日

NoClassDefFoundError

NoClassDefFoundError

NoClassDefFoundErrorに関する仕掛けは、次のサイトに書かれてある。
Androidアプリ実行時にNoClassDefFoundError
実行時にクラスパスが通ってない場合に発生するそうです。

私の場合は、Libsフォルダー内に、本来あるべきjarファイルを復活させたら治りました。

発生原因は、Libsフォルダー内の、何かのjarファイルを誤って削除してしまっていたことのようです。

2013年2月15日金曜日

Access Not Configured

Access Not Configured

detailMessage "403 Forbidden\n{\n  "code" : 403,\n  "errors" : [ {\n    "domain" : "usageLimits",\n    "message" : "Access Not Configured",\n    "reason" : "accessNotConfigured"\n  } ],\n  "message" : "Access Not Configured"\n}" (id=xxxxxxxxxx)

上記エラーに出会うかもしれない。もし、出会ったなら、keyの作成に問題があったのかもしれない。
APIs Consoleにアクセスして、
API Access>Simple API Access>Delete key...>Delete key
Create new Android Key...>空欄には何も記入せず>Create
これで再度チャレンジしてみよう。

2013年2月10日日曜日

BitmapFactory.Options inSampleSize

BitmapFactory.Options inSampleSize

私は、inSampleSizeに3を代入すれば、(Bitmapの大きさが)3分の1になるものと思い込んでいました。しかし、挙動が変なので、調べてみたのです。

BitmapFactory.Options inSampleSizeに関しては、次のサイトで動作検証がなされています。
BitmapFactory.Options inSampleSize の性能テストとか動作とか
貴重なデータを公開して頂きありがとうございます。手間が省けました。
上記サイトでの検証結果では、inSampleSizeに3を代入しても(Bitmapの大きさが)2分の1になってしまうのです。むむむ!

検証結果からすると、inSampleSizeに関して次のように解釈できます。
inSampleSizeを用いて縮小する場合、inSampleSizeに代入されている値の小さい方の直近の「2の累乗の倍数」で縮小される。
例えば、inSampleSizeに10を代入したとしても、(10分の1になるのではなく)8分の1に縮小されるのです。

念のため、inSampleSizeに関する留意点を書いておきます。

  1. inSampleSizeに関する仕掛けは、メモリの節約を目的としている。
  2. inSampleSizeには、縮小する倍率を代入する。例えば、2分の1に縮小したい場合には2を代入する。
  3. inSampleSizeは、int型であるため(float型では無いという意味で)大雑把な縮小しかできない。
  4. inSampleSizeは、上記の検証結果のとおり、大雑把な縮小しかできない。
  5. (inSampleSizeを使って作成された)この大雑把に縮小されたBitmapに対して、必要に応じて、本来目的としていたサイズに(更に微調整して)縮小する。
  6. 縮小する大きさが2分の1よりも大きい場合(例えば、3分の2の大きさに縮小したい場合)、inSampleSizeに関する仕掛けを使うことに意味は無い。この場合、別の仕掛けで縮小する。



2013年1月27日日曜日

画面サイズに応じてAdMobのサイズを変える

画面サイズに応じてAdMobのサイズを変える

従前は、画面が縦位置か横位置かによって、また、使われる端末の画面の大きさによって、AdMobの幅や位置を調整しなければならなかった(しなくても良いのだが)。

だが、これは不便だ。以前私は、このような不便に対する不満をこのブログに書いておいた。AdMobを中央に配置する。
そうしたところ、この要望に応えて頂いたのが、Smart Bannersなのである(私の要望に応えた訳では無いだろうが)。

Smart Bannersを使うことによって、AdMobは自動的に自分でサイズが変わることができる。これにより、プログラマが画面のサイズを取得してAdMobのサイズや位置を調整する必要は無くなった。自動的に、横中央に配置してくれるのである。

sample codeは次のとおりである。
    AdView adView;
    LinearLayout ll;
    AdRequest ar;
        
    //第二番目の引数にSMART_BANNERを指定する。
    //第三番目の引数にはAdMob publisher IDを指定する。
    adView = new AdView(this, AdSize.SMART_BANNER, "xxxxxxx");
    ll = (LinearLayout)findViewById(R.id.AdSettings);  
    ll.addView(adView);
    ar = new AdRequest();
    adView.loadAd(ar);