これは、別のアプリケーションでそのファイルを使用するために、画像の「ライブラリ」を外部ストレージに保存するために作成した平和なコードです。これは、オブジェクトの ArrayList を含むバイナリ ファイルです。
これは、主な仕事をするメソッドです。
public void createLib()
{
File fl = new File("/mnt/sdcard/imgs");
File[] rawLib = fl.listFiles();
TextView text = (TextView) findViewById(R.id.txt1);
ArrayList<Block> myList = new ArrayList<Block>();
try{
for (int i = 0; i < rawLib.length; i++)
{
FileInputStream fis = new FileInputStream(rawLib[i]);
Bitmap bmp = BitmapFactory.decodeStream(fis);
Block tmpBlock = new Block();
tmpBlock.bmp = bmp;
tmpBlock.mozColor = findMidColor(bmp);
myList.add(tmpBlock);
}
}
catch(Exception exc)
{
exc.printStackTrace();
}
try
{
String root = Environment.getExternalStorageDirectory().toString();
File myDir = new File(root + "/saved_images");
myDir.mkdirs();
File file = new File (myDir, "library.lib");
FileOutputStream fos = new FileOutputStream(file);
ObjectOutputStream save = new ObjectOutputStream(fos);
save.writeObject(myList);
save.close();
}
catch (Exception exc)
{
exc.printStackTrace();
}
ここに私が取り組んでいるクラスがあります
class Block
{
Bitmap bmp;
int mozColor;
}
findMidColor() は私の方法であり、かなりうまく機能するので、問題はありません。
エミュレータの外部ストレージから作成したファイルをプルすると、ファイルのサイズは約 2.5 キロバイトですが、画像を含む元のフォルダは約 2 ~ 3 メガバイトです。結論は、プログラムはその bmp へのポインターのみを保存するということです。画像と int を含むオブジェクトのバイナリ ファイルを作成し、そのファイルを ArrayList やその他の配列などの別のアプリケーションで再利用する方法はありますか?