このエントリーをはてなブックマークに追加

2010年4月25日日曜日

Android Intent to Take a Picture

[In English]
When we develop an application, sometimes we use camera function for it.
Of course we can implement our own camera function to our application using camera API, but most of Android Phone provide Camera application.
so today I'll show how to start up camera application and retrieve picture data from it using Intent.


final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);


We need to call Activity using startActivityForResult (not startActivity) to retrieve picture data.


protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK) {
// get the picture
final Bitmap bitmap = (Bitmap) data.getExtras().get("data");
// do something for bitmap data
final String sUri = MediaStore.Images.Media.insertImage(
getContentResolver(), bitmap, timestamp, timestamp);
…
…
…
}
}


After taking a picture using Camera application, onActivityResult will be called. And then we can get picture data as Bitmap from extras.
But I feel the quality of bitmap data is not good in this case, because data size in intent extras is limited.



[In Japanese]
アプリケーションを作るときに、場合によってはカメラ機能を使うときもあると思います。
その際に、もちろんカメラAPIを使って独自にカメラ機能を実装することもできますが、ほとんどのAndroid携帯はカメラアプリを提供しています。
そこで、今日はIntentを使ってカメラアプリを起動させ、撮影した写真データを取得する方法を紹介します。


final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, REQUEST_CAMERA);


この場合、写真データを取得するために、startActivityではなく、startActivityForResultを使う必要があります。


protected void onActivityResult(final int requestCode, final int resultCode, final Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CAMERA && resultCode == RESULT_OK) {
// get the picture
final Bitmap bitmap = (Bitmap) data.getExtras().get("data");
// do something for bitmap data
final String sUri = MediaStore.Images.Media.insertImage(
getContentResolver(), bitmap, timestamp, timestamp);
…
…
…
}
}


カメラアプリで写真を撮った後、onActivityResultが呼び出されます。そこで、写真データをBitmapとしてextrasから取得できます。
ただ、このBitmapのクオリティはあまりよくないように感じました、おそらくはIntentで渡せるデータサイズに制限があるためだと思います。

2010年4月19日月曜日

how to make kind of Twitter edit box

[In English]
Today, I'm going to make a component something like twitter edit box.
You know, edit box in http://twitter.com indicates the number of words that you can enter in real time.
EditText in Android SDK is not supported this function directory, so we need to implement similar function for ourselves.
We tend to think we can make it by applying a OnKeyListener to EditText.
But we can't in that way.

Basically OnKeyListener will inform you of hardware-keyboard events. But even in software-keyboard, OnKeyListener lets you know typical key events such as 'enter' or 'del'. But you cannot acquire whole events in software-keyborad.

In order to solve this problem, we create a new class extending EditText and then override onTextChanged method.
The method of 'onTextChanged' is called every time edit text is changed. Whichever software-keyboard or hardware-keyboard, you don't have to worry.

That way is very convenient solution this time. I wonder why onTextChanged method is provided as a listener. I think it's more useful for many occasions.

By the way, sample code is like this.


public class WordCountEditText extends EditText {

private TextView wordCountView;

private final static int MAX_LENGTH = 140;

public WordCountEditText(final Context context) {
super(context);
}

public WordCountEditText(final Context context, final AttributeSet attrs) {
super(context, attrs);
}

public WordCountEditText(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
}

@Override
protected void onTextChanged(final CharSequence text, final int start, final int before, final int after) {
if (wordCountView != null) {
final int textColor;
int leftLength = MAX_LENGTH - text.length();
if (leftLength < 0) {
textColor = Color.RED;
} else {
textColor = Color.GRAY;
}
wordCountView.setTextColor(textColor);
wordCountView.setText(String.valueOf(leftLength));
}
}

public void setWordCountView(final TextView wordCountView) {
this.wordCountView = wordCountView;
setText(getText());
}
}


And sample code in using class above.


WordCountEditText wordCountEditText = (WordCountEditText)findViewById(R.id.edit_text);
TextView wordCountView = (TextView)findViewById(R.id.word_count_view);
wordCountEditText.setWordCountView(wordCountView);






[In Japanese]
今日は、Twitter風のエディットテキストを作成する方法をご紹介します。
ご存知の通り、http://twitter.com で提供されているエディットボックスは、入力可能な残りの文字数がリアルタイムで表示されます。
残念ながら、Android SDKで提供されているEditTextには、この機能はありません。なので自分達で似たような機能を実装する必要があります。
Android SDKに慣れている人の中には、EditTextにOnKeyListenerを登録する事で実現できると考える人も多いと思います。しかしこのOnKeyListenerは、主にハードウェアキーボードのイベントを通知する為のものです。
ソフトウェアキーボードでも、「確定」キーや「DEL」キーのような代表的なキーのイベントはOnKeyListenerでイベントの検知が可能ですが、全てのイベントを検知する事はできません。

この問題を解決する為には、EditTextを継承した新しいクラスを作成し、onTextChangedメソッドをオーバーライドします。
onTextChangedメソッドは、EditTextの内容が書き換わる度に必ず呼び出されます。ハードウェアキーボードによる変更かソフトウェアキーボーかを気にする必要がありません。

今回の問題を解決するには正にうってつけの方法です。予断ですが、なぜonTextChangedメソッド相当のListenerが提供されていないのかが不思議です。Listenerが提供されていれば、新しいクラスを作成する必要もありませんし、色々な場面で便利だと思うのですが。

とりあえず、サンプルソースは以下のようになります。


public class WordCountEditText extends EditText {

private TextView wordCountView;

private final static int MAX_LENGTH = 140;

public WordCountEditText(final Context context) {
super(context);
}

public WordCountEditText(final Context context, final AttributeSet attrs) {
super(context, attrs);
}

public WordCountEditText(final Context context, final AttributeSet attrs, final int defStyle) {
super(context, attrs, defStyle);
}

@Override
protected void onTextChanged(final CharSequence text, final int start, final int before, final int after) {
if (wordCountView != null) {
final int textColor;
int leftLength = MAX_LENGTH - text.length();
if (leftLength < 0) {
textColor = Color.RED;
} else {
textColor = Color.GRAY;
}
wordCountView.setTextColor(textColor);
wordCountView.setText(String.valueOf(leftLength));
}
}

public void setWordCountView(final TextView wordCountView) {
this.wordCountView = wordCountView;
setText(getText());
}
}


で、上記のクラスを使う側のコードが以下です。


WordCountEditText wordCountEditText = (WordCountEditText)findViewById(R.id.edit_text);
TextView wordCountView = (TextView)findViewById(R.id.word_count_view);
wordCountEditText.setWordCountView(wordCountView);

2010年4月8日木曜日

Android Intent to show Location Settings View

[In English]
LocationManager is used in Android to get device location and show the address.
But if location setting is disabled on the device settings, Provider will be NULL.


final LocationManager locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
final Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
final LocationManager locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
final String provider = mLocationManager.getBestProvider(criteria, true);


In the case of the above sample, it is okay only to notify the user with Dialog, but I think it is better to move to Location Settings View because our applications can call the other applications using Intent on Android.


final Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(i);


Like this if you use Intent, you will not need to implement all function you want to your applications.
I'll continue to write blogs about Intent of Camera, Gallery and so on.



[In Japanese]
Androidアプリで位置情報を取得して、住所などを表示させる場合には、LocationManagerを使用します。
ただ、端末の設定で位置情報の送信を許可していないとProviderとして、nullが返されます。


final LocationManager locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
final Criteria criteria = new Criteria();
criteria.setAccuracy(Criteria.ACCURACY_FINE);
final LocationManager locationManager = (LocationManager)getSystemService(LOCATION_SERVICE);
final String provider = mLocationManager.getBestProvider(criteria, true);


nullが返された場合に、Dialogなどでユーザに通知するだけでもよいのですが、せっかくAndroidではIntentをつかって、他のアプリを呼び出せるので位置情報の設定画面へ遷移する方がよいと思います。


final Intent i = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(i);


このようにIntentを利用することで、他のアプリを呼び出せ、自分のアプリで全て実装する必要がなくなります。
今後もカメラやギャラリーなどの連携についてブログで紹介したいと思います。

2010年3月30日火曜日

All of three major mobile Carrier will release Android Phone in Japan

[in English]
Actually last year only one carrier Docomo has released Androd Phone in Japan, and few Japanese people except Developers know Android.

But in this year this situation will change greatly, because all of three Japanese major carrier will release Android Phone in this year.

NTT Docomo will release Xperia of Sony Ericson, KDDI release Sharp IS01 and the SoftBank will release HTC Desire.
(And Docomo and KDDI will release Android Market for Japanese Users, too.)


Application Security is very important, so sometimes Carrier check is necessary like iPhone Market of Apple.
But I hope that three Japanese carrier will continue Android Open idea, and also hope personal developers can release their applications with easy procedure to Japanese Carrier Market, too.

Anyway, I am bubbling with this Japanese Change.
And I already have two applications, and will release new one in this April.

I will be watching Japanese Android situation as user and also developer.



[in Japaese]
昨年は、日本ではドコモのみがAndroid携帯をリリースして、開発者を除くとほとんど、Androidは認知されていませんでした。
でも、今年はこの状況が大きく変わりそうです。というのも、日本の3大キャリア全てがAndroid携帯をリリースするからです。

ドコモからはソニーエリクソンのXperiaが、KDDIからはシャープののIS01、そしてソフトバンクからはHTC Desireがリリースされます。
(ドコモとKDDIに関しては、マーケットまで公開する予定のようです。)


アプリケーションのセキュリティ対策は大切だと思います、そのためAppleのiPhoneマーケットのようにキャリアチェックが必要な時もあると思います。
ただ、日本の3キャリアがAndroidのオープン思想は継続してほしいと思っています、また個人の開発者が簡単な手続きだけでキャリアのマーケットへも公開できるようにしてほしいと願っています。

いずれにしても、この日本のAndroidの状況変化にわくわくしています。
私は、既に2つのアプリケーションをリリースしていて、この春に新しいアプリをリリース予定です。

今後も日本のAndroidの市場状況をユーザとして、また開発者としてウォッチしていきたいと思います。

2010年3月22日月曜日

Emoji

[In English]
Do you know emoji?
Emoji is very popular in Japan especially among young women.
It's mainly used in e-mails in order to supplement emotion or decorate e-mails.

Now this time, I'm going to introduce the way to use emoji in android devices.
As a prerequisite to use emoji, the devices need to support emoji.
The android devices that are sold in Japan have the prerequisite, but the Android Emulator or Dev Phone does not.

And all you need to do is add the code below.

EditText editText = (EditText)findViewById(R.id.edit);
Bundle bundle = editText.getInputExtras(true);
if (bundle != null) {
bundle.putBoolean("allowEmoji", true);
}


That's it.

If you use EditText in your application, EditText would come in handy for Japanese by adding the code above. So let's give it a try.



[In Japanese]
絵文字という単語を知ってますか?
絵文字は感情を補完したり、メールを華やかにする為に用いられ、若い女性を中心に日本ではとても人気があります。

今回は、Android端末で絵文字を使用する方法をご紹介します。
まず前提条件として、その端末が絵文字をサポートしている必要があります。日本で販売されているAndroid端末ではサポートされていますが、開発者が使用するAndroidエミュレーターやDev Phoneなどではサポートされていません。

実装的には以下のコードを追加するだけです。

EditText editText = (EditText)findViewById(R.id.edit);
Bundle bundle = editText.getInputExtras(true);
if (bundle != null) {
bundle.putBoolean("allowEmoji", true);
}


アプリケーション内でEditTextを使用する場合は、上記のコードを追加するだけで、日本人にとっては非常に使い勝手が良くなります。ぜひ試してみて下さい。