###前提・実現したいこと
androidのステータスバーに表示するアイコンについての質問です。
API23以降ではNotificationの
.setSmallIcon(Icon icon)
と直接アイコンを指定できると公式には書いてあります。
そこでビットマップにテキストを書き込み、動的にアイコンに情報を
表示するようにしようとしています。
開発はAndroidStudioで行っておりますが、エミュレーターでは
問題なくアイコンが表示されますが、実機(Xperia Z5 Premium)
ではエラーが出ます。このソースでは簡略化してTSTと書き込んでいます。
どなたかエラーの原因がお分かりであればご教示願えませんでしょうか。
###発生している問題・エラーメッセージ
問題が発生したため[システムUI]を終了します
###該当のソースコード
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //適当なビットマップを用意 Bitmap bitmap = Bitmap.createBitmap(72, 72, Bitmap.Config.ARGB_8888); //TSTと書き込む Canvas canvas = new Canvas(bitmap); canvas.drawARGB(0, 0, 0, 0xFF); TextPaint textPaint = new TextPaint(); textPaint.setAntiAlias(true); textPaint.setColor(Color.BLACK); textPaint.setFakeBoldText(true); textPaint.setTextSize(24); canvas.drawText("TST", 0, 72, textPaint); Icon icon; icon = Icon.createWithBitmap(bitmap); // 動的に描画した画像からアイコンを作成 // 通知 Notification ntf = new Notification.Builder(this) .setContentTitle("test") .setContentText("testtesttest") .setSmallIcon(icon) // API23以降はIconも指定できるはず //.setSmallIcon(R.drawable.ic_stat_name) これは動きます .setLargeIcon(bitmap) .build(); NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); nm.notify(1, ntf); } }
###試したこと
動的にIcon iconではなく従来の表示方法では適当に絵は出せます。
デバッガで動作を追ったところ最後の一行のnotifyでエラーが出ます。
公式を見ても.setSmallIcon(Icon icon)については特に注意事項も見当たらず手詰まりの状態です。
追記1 (3/31 23:16)
logcatのログを更新しました。実機はXperia C5 Ultra(android 6.0)を使いましたが現象は同じです。
03-31 23:14:51.791 7743-7743/? I/art: Late-enabling -Xcheck:jni 03-31 23:14:51.843 7743-7743/com.example.t_oot.sbtest I/ActivityThread: Switching default density from 480 to 360 03-31 23:14:51.854 7743-7743/com.example.t_oot.sbtest W/System: ClassLoader referenced unknown path: /data/app/com.example.t_oot.sbtest-1/lib/arm64 03-31 23:14:51.975 7743-7743/com.example.t_oot.sbtest W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 03-31 23:14:52.054 7743-7752/com.example.t_oot.sbtest I/System: FinalizerDaemon: finalize objects = 1 03-31 23:14:52.148 7743-7743/com.example.t_oot.sbtest I/[MALI][Gralloc]: [+]r_hnd(0x7fa76378e0), client(39), share_fd(37) 03-31 23:14:52.200 7743-7784/com.example.t_oot.sbtest E/GED: Failed to get GED Log Buf, err(0) 03-31 23:14:52.200 7743-7784/com.example.t_oot.sbtest I/OpenGLRenderer: Initialized EGL, version 1.4 03-31 23:14:52.205 7743-7784/com.example.t_oot.sbtest I/OpenGLRenderer: Get enable program binary service property (1) 03-31 23:14:52.205 7743-7784/com.example.t_oot.sbtest I/OpenGLRenderer: Initializing program atlas... 03-31 23:14:52.206 7743-7784/com.example.t_oot.sbtest I/OpenGLRenderer: Program binary detail: Binary length is 146348, program map length is 128. 03-31 23:14:52.207 7743-7784/com.example.t_oot.sbtest I/OpenGLRenderer: Succeeded to mmap program binaries. File descriptor is 44, and path is /dev/ashmem�. 03-31 23:14:52.207 7743-7784/com.example.t_oot.sbtest I/OpenGLRenderer: No need to use file discriptor anymore, close fd(44). 03-31 23:14:52.213 7743-7784/com.example.t_oot.sbtest W/libEGL: [ANDROID_RECORDABLE] format: 1 03-31 23:14:52.225 7743-7784/com.example.t_oot.sbtest I/PerfService: PerfServiceNative api init 03-31 23:14:52.235 7743-7784/com.example.t_oot.sbtest I/[MALI][Gralloc]: [+]r_hnd(0x7fb1c67c80), client(39), share_fd(46) 03-31 23:14:52.253 7743-7743/com.example.t_oot.sbtest I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@799da7c time:1133912660
追記2 (3/31 23:16)
Gradleの内容です。AndroidStudioで空のアプリを生成して内容はいじっていません。
apply plugin: 'com.android.application' android { compileSdkVersion 25 buildToolsVersion "25.0.2" defaultConfig { applicationId "com.example.t_oot.sbtest" minSdkVersion 23 targetSdkVersion 25 versionCode 1 versionName "1.0" testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" } buildTypes { release { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) androidTestCompile('com.android.support.test.espresso:espresso-core:2.2.2', { exclude group: 'com.android.support', module: 'support-annotations' }) compile 'com.android.support:appcompat-v7:25.3.1' testCompile 'junit:junit:4.12' }
回答3件
あなたの回答
tips
プレビュー