アプリ作って海外移住

コピペプログラマーのメモ

bitmap について  読み込み時サイズを先に取得>読み込みサイズ決定>読み込み

読み込み
 
BitmapFactory.Options opt = new BitmapFactory.Options();
    // オプション設定用のオブジェクト
opt.inJustDecodeBounds = true;
    // 実際の画像本体は読まずにサイズ情報のみ取得するフラグをセット
 
BitmapFactory.decodeFile(path, opt);
    // サイズ情報を取得する
int scaleW = opt.outWidth / 300; // →2
int scaleH = opt.outHeight / 200; // →3
    // 取得した画像サイズは
    // BitmapFactory.Options#outWidth
    // BitmapFactory.Options#outHeight
    // にそれぞれ入り、表示サイズで割ることで縮小時の整数比率を求める
 
int sampleSize = Math.max(scaleW, scaleH); // →3
opt.inSampleSize = sampleSize;
    // 画像を何分の1で取得するかを求めます
    // 2の乗数に丸められるのでここでは3で指定しても2と同様に扱われる
opt.inJustDecodeBounds = false;
    // 今度は実際に画像を取得するためのフラグをセット
Bitmap bmp = BitmapFactory.decodeFile(path, opt);
    // inSampleSizeに3を指定し、処理としては2の乗数に丸められるので2
    // つまり元画像の1/2で読み込まれる
int w = bmp.getWidth(); // →400
int h = bmp.getHeight(); // →300
float scale = Math.min((float)300/w, (float)200/h); // →0.6666667
    // 最終的なサイズにするための縮小率を求める
Matrix matrix = new Matrix();
matrix.postScale(scale, scale);
    // 画像変形用のオブジェクトに拡大・縮小率をセットし
bmp = Bitmap.createBitmap(bmp, 0, 0, w, h, matrix, true);
    // 取得した画像を元にして変形画像を生成・再設定

 

 

 

 

 

 

 

 

 

 

 

 

//画像をファイルとして取り出す 表示

 

File insamu = new File(Target_Samupath);
FileInputStream fis;
try {
fis = new FileInputStream(insamu);
Bitmap bm = BitmapFactory.decodeStream(fis);
iv1.setImageBitmap(bm);

} catch (FileNotFoundException e) {
// TODO 自動生成された catch ブロック
iv1.setImageResource(R.drawable.ws3);
}

 

//画像ファイルの保存

bitmap の nullチェックが必要。

NULLの場合 fos = new FileOutputStream(E12);

       Target_Samubitmap.compress(CompressFormat.JPEG, 100, fos); 

を実行するとbitmapがNULLで保存されてしまう。そのあとにcatch (Exception e)が実行するが、遅いちゅうの!!

if(Target_Samubitmap == null){

}else{
//画像ファイルの保存
try {

// 保存処理開始
FileOutputStream fos = null;
fos = new FileOutputStream(E12);

// jpegで保存

Target_Samubitmap.compress(CompressFormat.JPEG, 100, fos);

// 保存処理終了
fos.close();
} catch (Exception e) {
Log.v("gazou is not update","addactivity java 620");

}

}

 

 

これダメ---------------------

try {

// 保存処理開始
FileOutputStream fos = null;
fos = new FileOutputStream(Target_Samupath);

// jpegで保存
Target_Samubitmap.compress(CompressFormat.JPEG, 100, fos);

// 保存処理終了
fos.close();
} catch (Exception e) {
Log.e("Error", "" + e.toString());
}

 

 

//ファイルの存在確認

File file2 = new File(Old_Samupath);
if (file2.exists()){

//対象ファイルの削除
removeThumb();
}

 

 

 //対象ファイルの削除

File file2 = new File(Old_Samupath);
if (file2.exists()){file2.delete();
}

 

 

gallery の反映はわからない!!  考え中

/* ContentResolver contentResolver = getContentResolver();
ContentValues values = new ContentValues();
values.put(Images.Media.TITLE, "trimage"+Target_Trno);
values.put(Images.Media.DISPLAY_NAME, Target_BMpath);
values.put(Images.Media.DATE_TAKEN, System.currentTimeMillis());
values.put(Images.Media.MIME_TYPE, "image/jpeg");
values.put(Images.Media.ORIENTATION, 0);
values.put(Images.Media.DATA, Target_BMpath);

contentResolver.insert(Images.Media.EXTERNAL_CONTENT_URI, values);
*/

//gallery チェック処理を走らせると時間がかかるし、その間、android は止まってくれないから、次の画像処理で落ちる