▼Androidメモ▼
スナップショットの保存


スナップショットをファイルに保存するプログラムを作成する。保存先は「/data/data/net.npaka.snapshotex/files/snapshot.jpg」。



ソースコード
SnapShotEx.java
package net.npaka.snapshotex;
import android.app.*;
import android.content.*;
import android.graphics.*;
import android.os.*;
import android.view.*;
import android.widget.*;
import java.io.*;

//スナップショットの保存
public class SnapShotEx extends Activity
    implements View.OnClickListener {
    private Button button;//ボタン
    
    //初期化
    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        requestWindowFeature(Window.FEATURE_NO_TITLE);

        //レイアウトの生成
        AbsoluteLayout layout=new AbsoluteLayout(this);
        layout.setBackgroundColor(Color.rgb(255,255,255));
        setContentView(layout); 

        //ボタンの生成
        button=makeButton(this,"SnapShot",0,0);
        layout.addView(button);    
    }

    //ボタンの生成
    private Button makeButton(Context context,
        CharSequence label,int x,int y) {
        Button button=new Button(this);
        button.setText(label);
        button.setLayoutParams(new AbsoluteLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,x,y));
        button.setOnClickListener(this);    
        return button;
    }

    //ボタンクリックイベントの処理
    public void onClick(View v) {
        if (v==button) {
            //スナップショット
            View view=button.getRootView();
                view.setDrawingCacheEnabled(true);
            Bitmap bitmap=view.getDrawingCache();
            if (bitmap==null) return;

            //ファイルに保存
            try {
                byte[] w=bmp2data(bitmap,Bitmap.CompressFormat.JPEG,80);
                writeDataFile("snapshot.jpg",w);
            } catch (Exception e) {
                android.util.Log.e("",e.toString());
            }
        }
    }
    
    //Bitmap→バイトデータ
    private static byte[] bmp2data(Bitmap src,
        Bitmap.CompressFormat format,int quality) {
        ByteArrayOutputStream os=new ByteArrayOutputStream();
        src.compress(format,quality,os);            
        return os.toByteArray();
    }
   
    //ファイルへのバイトデータ書き込み
    private void writeDataFile(String name,byte[] w) throws Exception {
        OutputStream out=null;
        try {
            out=openFileOutput(name,Context.MODE_WORLD_READABLE);
            out.write(w,0,w.length);
            out.close();
        } catch (Exception e) {
            try {
                if (out!=null) out.close();
            } catch (Exception e2) {
            }
            throw e;
        }
    }
}


バイトデータファイルのBitmap化
    //バイトデータ→Bitmap
    private static Bitmap data2bmp(byte[] data) {
        return BitmapFactory.decodeByteArray(data,0,data.length);
    }    
    
    //ファイルからのバイトデータ読み込み
    private byte[] readDataFile(String name) throws Exception {
        InputStream in=null;
        try {
            in=openFileInput(name);
            byte[] w=new byte[102400];
            int size=in.read(w);
            in.close();
            byte[] result=new byte[size];
            System.arraycopy(w,0,result,0,size);
            return result;
        } catch (Exception e) {
            try {
                if (in!=null) in.close();
            } catch (Exception e2) {
            }
            throw e;
        }
    }



−戻る−