カテゴリー
SugiBlog Webデザイナー・プログラマーのためのお役立ちTips

Zip形式圧縮ファイルを解凍する Android

Androidで、Zip形式の圧縮ファイルを解凍します。

インポート

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.BufferedOutputStream;

import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

必要な変数の宣言

ZipInputStream in = null;
String zipPath = null;
ZipEntry zipEntry;
int data;
String file_name;

続きを読む…»

6,578 views

Bluetoothを扱う Android

AndroidでBluetoothを使ってみます。

Bluetoothアダプタークラスをインポート

import android.bluetooth.BluetoothAdapter;

アダプターとリクエストコードの定義

BluetoothAdapter mBluetoothAdapter = null;
final int REQUEST_ENABLE_BT = 1; //任意のコード

アクティビティのonCreateでアダプターをセットします。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

	mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
}

続きを読む…»

7,147 views

スマートフォン対応デザイン

当サイトのスマートフォン対応デザインが完成しました。

iPhoneやAndroid端末でアクセスするとそちらが表示されるようになっています。

1,277 views

SDカードにフォルダーを作成する mkDir

SDカード内にフォルダーを作成します。
再帰的に作成するにはmkDirsメソッドを使用します。
※パーミッションの記述は割愛します。

String PATH = Environment.getExternalStorageDirectory().toString() + "/DCIM/Example";

File f = new File(PATH);

if(!f.exists()) {
    try {
        f.mkdir();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
6,215 views

JSONデータを扱う [Android]

以下のようなJSONデータがあったとします。

{"json":[{"id": "00001","category": "カテゴリー1","title": "テスト1"},{"id": "00002","category": "カテゴリー2","title": "テスト2"}]}
String json_data = "{\"json\":[{\"id\": \"00001\",\"category\": \"カテゴリー1\",\"title\": \"テスト1\"},{\"id\": \"00002\",\"category\": \"カテゴリー2\",\"title\": \"テスト2\"}]}";

JSONArray jArray;
StringBuilder sb = new StringBuilder();

try {
    jArray = new JSONObject( json_data ).getJSONArray( "json" );

    for (int i = 0; i < jArray.length(); i++) {
        JSONObject jsonObj = jArray.getJSONObject(i);
        sb.append(jsonObj.getString("title"));
    }

    Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();

} catch (JSONException e) {
    e.printStackTrace();
} catch (Exception e) {
    e.printStackTrace();
}

コード上でデータを追加する場合(例外処理は上記と同様にしてださい)

JSONObject nJArray = new JSONObject();
nJArray.put("id", "00003");
nJArray.put("category", "カテゴリ3");
nJArray.put("title", "テスト3");
jArray.put(nJArray);
10,319 views