suer TIL
eye catch
複数回のコマンドの標準出力をまとめて一度にリダイレクトする
複数回のコマンドを同じファイルにリダイレクト 以下のようなシェルスクリプトを書いた場合、同じファイルをコマンドの回数だけオープンすることになり 無駄が生じる。 #!/bin/sh echo foo >> a.txt echo bar >> a.txt echo baz >> a.txt shellcheckにかけると以下の違反が検出される。
LINUX
2022-08-28 2022-08-28
eye catch
【Android,Kotlin】端末に保存されたファイルかカメラ撮影から画像選択できるようにする
ファイル選択orカメラ撮影で画像を選択できるようにしたい こういうやつ。 カメラで撮った写真を使う場合と端末のファイルを利用する場合でそれぞれ単体の実装は以下の記事を参照。 カメラ: 端末で写真を撮って表示する ファイル選択: GetContentで端末に保存された画像ファイルを選択して表示する OpenDocumentで端末内の画像ファイルを選択して表示する 実装 自作ActivityResultContract ボトムシートとしてファイル選択、カメラ撮影から選択できるようにするためのActivityResultContractを作成する。
ANDROID
2022-08-27 2026-08-15
eye catch
【Android,Kotlin】OpenDocumentで端末内の画像ファイルを選択して表示する
アプリのイメージ 端末に保存されている画像を選択したい。 関連: 【Android,Kotlin】GetContentで端末に保存された画像ファイルを選択して表示する 実装 レイアウト ImageViewとボタンを設置しておく。 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="300dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/next" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/imageView" /> </androidx.constraintlayout.widget.ConstraintLayout> Fragment/ViewModel フィールドにregisterForActivityResultでActivityResultContracts.OpenDocument()のlauncherを作る。
ANDROID
2022-08-23 2023-03-31
eye catch
【Android,Kotlin】端末で写真を撮って表示する
アプリのイメージ カメラアプリで写真を撮って表示したい。 実装 レイアウト ImageViewとボタンを設置しておく。 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="300dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/next" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/imageView" /> </androidx.constraintlayout.widget.ConstraintLayout> cacheフォルダへの書き込みを許可する 外部のカメラアプリがアプリのcacheフォルダへ書き込み、アプリはそれを画面に表示する。
ANDROID
2022-08-23 2026-08-15
eye catch
【Android,Kotlin】GetContentで端末に保存された画像ファイルを選択して表示する
アプリのイメージ 端末に保存されている画像を選択したい。 関連: 【Android,Kotlin】OpenDocumentで端末内の画像ファイルを選択して表示する 実装 レイアウト ImageViewとボタンを設置しておく。 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:id="@+id/imageView" android:layout_width="match_parent" android:layout_height="300dp" app:layout_constraintTop_toTopOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintEnd_toEndOf="parent"/> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/next" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toBottomOf="@id/imageView" /> </androidx.constraintlayout.widget.ConstraintLayout> Fragment/ViewModel フィールドにregisterForActivityResultでActivityResultContracts.GetContent()のlauncherを作る。
ANDROID
2022-08-23 2023-03-31
eye catch
【Android,Kotlin】Kotlinらしいダウンキャスト
ダウンキャストが必要となる例 open class Super {} class A : Super() { fun f() {} } val x: Super = A() x.f() // x は Super なので f メソッドがわからずエラー: if+is演算子で分岐する Kotlinでは実行箇所の文脈によって自動的に推論されてダウンキャストされる。 例えば上記例では if (x is A) でチェックすると、そのなかではxはAのインスタンスであると判断されるので Aクラスのインスタンスメソッドf()が呼べるようになる。
ANDROID
2022-08-22 2023-03-31
eye catch
【Android,Kotlin】KtlintでKotlinコードのチェック
Ktlint KtlintはKotlin用のlinter。 本記事ではビルド設定(Gradle)に組み込んで使用する。 app/build.gradleに設定を追加する configurations { ktlint } dependencies { ... // バージョンは本家を確認すること https://github.com/pinterest/ktlint ktlint "com.pinterest:ktlint:0.48.2" } // lintタスクを追加 task ktlint(type: JavaExec, group: "verification") { description = "Check Kotlin code style." classpath = configurations.ktlint main = "com.pinterest.ktlint.Main" args "src/**/*.kt" } check.dependsOn ktlint // フォーマットタスクを追加 task ktlintFormat(type: JavaExec, group: "formatting") { description = "Fix Kotlin code style deviations." classpath = configurations.ktlint main = "com.pinterest.ktlint.Main" args "-F", "src/**/*.kt" } argsに指定しているパスはプロジェクトの環境に合わせる。
ANDROID
2022-08-21 2026-08-15
eye catch
【HUGO】シンタックスハイライトのスタイルを変更する
デフォルトのハイライトが視認性が悪い hugoで作られた当サイトのソースコードハイライトでコントラスト比の悪いところがあり、 見づらい箇所がところどころあるのでスタイルを変更したい。
HUGO
2022-08-21 2022-08-21
eye catch
【Java】Files.list()の戻り値Stream<Path>はclose()が必要
java.nio.Files.list(Path)でリストアップしたらクローズしないといけない java.nio.Files.list(Path)で指定したディレクトリ直下のファイルのパス一覧をStream<Path>型で取得できる Stream<Path> paths = Files.list(Paths.get("/path/to/directory")); このメソッドの戻り値をcloseしないとToo many open filesでエラーが発生するようになる。 java.nio.Files.list(Path)の戻り値をcloseする Stream<T>インタフェースの親インタフェースBaseStream<T>は、さらに親がAutoClosableインタフェースなので try-with-resoucesで確実に閉じることができる。
JAVA
2022-08-21 2022-08-21
eye catch
【Android,Kotlin】SoundPoolクラスを利用して効果音を鳴らす
SoundPoolクラスを利用する 本記事ではSoundPoolクラスを利用して効果音を鳴らす。 SoundPoolクラスはあらかじめ音声ファイルをデコードしてメモリに持っておく仕組みになっていてレイテンシが小さい。 速いフィードバックが求められる効果音の再生に向いている。
ANDROID
2022-08-20 2023-03-31
  • ««
  • «
  • 84
  • 85
  • 86
  • 87
  • 88
  • »
  • »»
AUTHOR
author image
suer
ふつうのプログラマ
LATESTS
eye catch
ISUCON2026に参加して敗退した
LINUX
2026-10-31 2026-11-04
eye catch
【cmp,dd】コピー先ファイルの壊れた範囲だけを書き直す
LINUX
2026-10-30 2026-10-30
eye catch
【shasum】コピー前後のファイルをSHA-256で比較して検証する (-a 256)
MAC
2026-10-30 2026-10-30
eye catch
【rsync】コピー結果をドライランで検証する (-n -c)
LINUX
2026-10-30 2026-10-30
eye catch
【rsync】接続エラーで停止する転送を自動でリトライする
LINUX
2026-10-30 2026-10-30
  • ホーム
  • お問い合わせ
  • プライバシーポリシー

© 2022 suer