ソース掲示板




すべてから検索

キーワード   条件 表示 現行ログ 過去ログ トピックス 名前 本文
ImageView を起点とした画像データの処理 / Android
日時: 2016/11/08 14:46
名前: lightbox



Bitmap から バイト配列Bitmap から ファイルバイト配列 から Firebase storage


▼ Bitmap に変換 する
ImageView のメソッド
拡張子:
ImageView imageView = (ImageView) findViewById(R.id.imageView);
Bitmap image = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
View のメソッド
拡張子:
// View から Bitmap 取得( v は、onClick の View )
View view = v.getRootView();				// 画面全体
view.setDrawingCacheEnabled(true);
Bitmap cache = view.getDrawingCache();			// 現在のキャッシュ
Bitmap rootViewCapture = Bitmap.createBitmap(cache);	// 新しいアプリ用の Bitmap 作成
view.setDrawingCacheEnabled(false);
※ ここでは、画面全体の Bitmap を取得しています 参考
メンテナンス

Bitmap から ByteArrayOutputStream を使用して byte 配列(画像データそのもの) を取得する ( No.1 )
日時: 2016/11/07 13:20
名前: lightbox


日時: 2016/11/07 13:20
名前: lightbox
拡張子:
// View から Bitmap 取得( v は、onClick の View )
View view = v.getRootView();	// 画面全体
view.setDrawingCacheEnabled(true);
Bitmap cache = view.getDrawingCache();
Bitmap rootViewCapture = Bitmap.createBitmap(cache);
view.setDrawingCacheEnabled(false);

// byte[] に変換
ByteArrayOutputStream baos = new ByteArrayOutputStream();
rootViewCapture.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] data = baos.toByteArray();
※ ここでは、PNG を作成しています 参考
このアーティクルの参照用URLをクリップボードにコピー メンテナンス
Bitmap を FileOutputStream を使用してファイルとして保存する(ギャラリーにも登録) ( No.2 )
日時: 2016/11/07 13:41
名前: lightbox
bmp2.compress(Bitmap.CompressFormat.JPEG,90,jpg);
拡張子:
FileOutputStream jpg;
try {
	// 保存場所で FileOutputStream を作成	
	jpg = new FileOutputStream(imagePath);
	if ( portrait ) {
		// 縦の場合、回転して保存する
		Bitmap bmp1 = BitmapFactory.decodeByteArray (data, 0, data.length);
		int width = bmp1.getWidth();
		int height = bmp1.getHeight();
		Matrix matrix = new Matrix();
		matrix.postRotate (90);
		Bitmap bmp2 = Bitmap.createBitmap (bmp1, 0, 0, width, height, matrix, true);
		// ▲ ここまでで、bmp1 を 90 度回転した、bmp2 を取得しています

		// Bitmap(bmp2) を FileOutputStream(jpg) でファイル保存
		bmp2.compress(Bitmap.CompressFormat.JPEG,90,jpg);

	}
	else {
		// ここは、byte[] を直接保存
		jpg.write(data);
		jpg.close();
	}

	// ギャラリーに反映
	MediaScannerConnection.scanFile(
		MainActivity.this,
		new String[] { imagePath },
		new String[] { "image/jpeg" },
		null);

} catch (Exception e) {
	e.printStackTrace();
}
参考
このアーティクルの参照用URLをクリップボードにコピー メンテナンス
byte 配列の画像データを Firebase storage に保存する ( No.3 )
日時: 2016/11/07 14:06
名前: lightbox
拡張子:
FirebaseStorage storage;
StorageReference storageRef;
StorageReference imageRef;

// --------------------------------------------------------

storage = FirebaseStorage.getInstance();
storageRef = storage.getReferenceFromUrl("gs://freebase-654b7.appspot.com/");

// 画像アップロード用パス決定
Calendar cal = Calendar.getInstance();
SimpleDateFormat sf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String uploadImagePath = String.format("image/%s.png",sf.format(cal.getTime()));
imageRef = storageRef.child(uploadImagePath);

// --------------------------------------------------------
// byte[] data にデータが格納されています

UploadTask uploadTask = imageRef.putBytes(data);
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
	@Override
	public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {

		Log.i("lightbox","アップロードに成功しました");
		long size = taskSnapshot.getMetadata().getSizeBytes();
		Log.i("lightbox",String.format("サイズ : %d",size));

	}
}).addOnFailureListener(new OnFailureListener() {
	@Override
	public void onFailure(@NonNull Exception e) {

		Log.i("lightbox","アップロードに失敗しました");
	}
});
参考
このアーティクルの参照用URLをクリップボードにコピー メンテナンス