ラベル assets の投稿を表示しています。 すべての投稿を表示
ラベル assets の投稿を表示しています。 すべての投稿を表示

2012年7月15日日曜日

assetsフォルダのフォントファイルの利用:Typeface


フォントサイズが大きすぎてassets フォルダで未圧縮の状態では使えない場合。
assets フォルダに圧縮したフォントファイルをおいて、アプリケーション領域にコピーして使う。

アプリの容量は、4GB にまで拡大されたが、APK ファイル自体のサイズは50MBまで、
拡張ファイル1つあたりの制限は2GB までなので2GBを超えるフォントサイズでは使えないので、WEBからダウンロードする方法を採らないといけないかも。

assetsフォルダの圧縮ファイルを解凍してアプリケーション領域のfilesフォルダにコピーする。
一つのファイルが圧縮されている場合。
try {
   AssetManager am = getResources().getAssets();
   InputStream is = am.open("ipaexm.zip", AssetManager.ACCESS_STREAMING);
   ZipInputStream zis = new ZipInputStream(is);
   ZipEntry  ze = zis.getNextEntry();

   if (ze != null) {
    path = getFilesDir().toString() + "/" + ze.getName();
    FileOutputStream fos = new FileOutputStream(path, false);
    byte[] buf = new byte[1024];
    int size = 0;

    while ((size = zis.read(buf, 0, buf.length)) > -1) {
     fos.write(buf, 0, size);
    }
    fos.close();
    zis.closeEntry();
   }
   zis.close();
  } catch (Exception e) {
   e.printStackTrace();
  }

圧縮ファイルに複数のファイルが含まれているとき
try {
   AssetManager am = getResources().getAssets();
   InputStream is = am.open("ipaexm.zip", AssetManager.ACCESS_STREAMING);
   ZipInputStream zis = new ZipInputStream(is);
   ZipEntry  ze = zis.getNextEntry();

   while (ze != null) { 
    path = getFilesDir().toString() + "/" + ze.getName();
    FileOutputStream fos = new FileOutputStream(path, false);
    byte[] buf = new byte[1024];
    int size = 0;

    while ((size = zis.read(buf, 0, buf.length)) > -1) {
     fos.write(buf, 0, size);
    }
    fos.close();
    zis.closeEntry();
    ze = zis.getNextEntry();
   }
   zis.close();
  } catch (Exception e) {
   e.printStackTrace();
  }

フォントファイルがアプリケーションデータ領域のfilesフォルダに保存されている場合
TextView tv2 = (TextView)findViewById(R.id.textView2);
TextView tv3 = (TextView)findViewById(R.id.textView3);

Typeface typeface = Typeface.createFromFile("/data/data/com.ymato.font/files/ipaexg.ttf");
Typeface typeface2 = Typeface.createFromFile("/data/data/com.ymato.font/files/ipaexm.ttf");

tv2.setTypeface(typeface);//
tv3.setTypeface(typeface2);//

フォントファイルがSDカードに入っている場合
Typeface typeface = Typeface.createFromFile( new File(Environment.getExternalStorageDirectory(), "ipaexm.ttf"));

assetsフォルダには1MB以上の非圧縮ファイルを設置できないので、
大きいサイズのフォントファイルを置いたらエラーが出た
Data exceeds UNCOMPRESS_DATA_MAX (6022748 vs 1048576)

FileOutputStream fos = new FileOutputStream(path, false); ではパーミッションが
-rw------- になるので変更する場合
FileOutputStream fos = openFileOutput( ze.getName() , MODE_WORLD_READABLE);
ファイルの指定方法が違う、上は/data/data/[app name]/files/fileName,
下は、ファイル名のみ、
MODE_PRIVATE このアプリからのみ使用
MODE_APPEND 現在のファイルに追記
MODE_WORLD_READABLE 他のアプリからも読み込み可能
MODE_WORLD_WRITEABLE 他のアプリからも書き込み可能

2011年12月26日月曜日

assetsフォルダのデータベースファイル利用

assetsフォルダのデータベース利用2 を新しく書きました。
assetsフォルダにdatabase.dbがある場合

assetsフォルダのデータベースをdatabaseフォルダにコピーして使う
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.app.Activity;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.widget.TextView;

public class RopouActivity extends Activity {

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  String DB_PATH = "/data/data/com.yamato.ropou/databases/";
  String DB_NAME = "database.db";

  try {

   InputStream myInput = getResources().getAssets()
     .open("database.db");
//InputStream myInput = this.getResources().openRawResource(R.raw.database);//res/rawフォルダにデータベースがある場合
   String outFileName = DB_PATH + DB_NAME;
   OutputStream myOutput = new FileOutputStream(outFileName);

   byte[] buffer = new byte[2048];
   int length;
   while ((length = myInput.read(buffer)) > 0) {
    myOutput.write(buffer, 0, length);
   }

   // Close the streams
   myOutput.flush();
   myOutput.close();
   myInput.close();

  } catch (IOException e) {
   // TODO 自動生成された catch ブロック
   e.printStackTrace();
  }

        //データベースを開く  
  SQLiteDatabase db = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, 0);
         
        Cursor c = db.query("myDatabaseTable", new String[] { "name", "age" },
          null, null, null, null, null);
         
        boolean isEof = c.moveToFirst();
        TextView textView1 = (TextView)findViewById(R.id.textView1);
         
        String text="";
        while (isEof) {
            text += String.format("%s : %d歳\r\n", c.getString(0), c.getInt(1));
            isEof = c.moveToNext();
        }
        textView1.setText(text);
         
        c.close();
        db.close();
    }
}


動かない
package com.android.word.net;

import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;

public class AndroidWordNetActivity extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  AssetManager as = getResources().getAssets();
  
  Log.d("タグ",as.toString() );
  
  //ヘルパークラスのインスタンスを作成します。
        MyDBHelper helper = new MyDBHelper(this,as.toString()+"database.db");
         
        SQLiteDatabase db = helper.getWritableDatabase();
        
        Cursor c = db.query("myDatabaseTable", new String[] { "name", "age" },
          null, null, null, null, null);
         
        boolean isEof = c.moveToFirst();
        TextView textView1 = (TextView)findViewById(R.id.textView1);
         
        String text="";
        while (isEof) {
            text += String.format("%s : %d歳\n", c.getString(0), c.getInt(1));
            isEof = c.moveToNext();
        }
        textView1.setText(text);
         
        c.close();
        db.close();
        
 }
 

 public class MyDBHelper extends SQLiteOpenHelper {
  public MyDBHelper(Context context, String string) {
            super(context, string, null, 1);
        }
  
        @Override
        //ここでデータベース作成(コンストラクタに渡されたDBファイル名が存在しない場合に呼ばれる)
        public void onCreate(SQLiteDatabase db) {
            // テーブルを作成
        }


  @Override
  public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
   // TODO 自動生成されたメソッド・スタブ
   
  }
 }
}

2011年12月25日日曜日

assetsフォルダのファイル利用

assetsフォルダに入れられるファイルサイズは1MBの制限がある。



package com.android.word.net;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

import android.app.Activity;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.widget.TextView;
import android.widget.Toast;

public class AndroidWordNetActivity extends Activity {
 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);

  AssetManager as = getResources().getAssets();

  BufferedReader br = null;
  StringBuilder sb = new StringBuilder();

  try {
   try {
    InputStream is = as.open("asset_file.txt");
    br = new BufferedReader(new InputStreamReader(is));

    String str;
    while ((str = br.readLine()) != null) {
     sb.append(str + "\n");
    }
   } finally {
    if (br != null)
     br.close();
   }
  } catch (IOException e) {
   Toast.makeText(this, "読み込み失敗", Toast.LENGTH_SHORT).show();
  }
  TextView label = (TextView) this.findViewById(R.id.textView1);
  label.setText(sb.toString());
 }
}