Android アプリ内に画像を保存する
Androidで画像を保存する方法としては以下の3パターンがあるかと思う1)アプリ内に保存
2)SDカード内に保存
3)データベースに保存
自分のアプリでも1)が一番よく使っている。
SLAMDUNKLineの背景やTextIconCreaterIconPackでアイコンを保存しているのは
アプリ内保存です。
Handromemoは2)の方法でSDカードに保存している。
3)は試したことないけれどバイナリで保存できるはず、、、きっと。
1)の方法で画像を保存する方法はこんな感じで簡単。
FileOutputStream out = null;
try {
// openFileOutputはContextのメソッドなのでActivity内ならばthisでOK
out = this.openFileOutput("image.png", Context.MODE_PRIVATE);
image.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (FileNotFoundException e) {
// エラー処理
} finally {
if (out != null) {
out.close();
out = null;
}
}
保存した画像の読み込みも簡単
InputStream input = null;
try {
input = this.openFileInput("image.png");
} catch (FileNotFoundException e) {
// エラー処理
}
Bitmap image = BitmapFactory.decodeStream(input);
保存した画像の削除
this.deleteFile("image.png");
この方法ならば、画像ファイルだけでなく
各ファイルなんでも保存できるので便利だし、
SDカード使用する際の権限等も不要。
登録:
コメントの投稿
(
Atom
)
0 件のコメント :
コメントを投稿