21

メッセージ、タイトル、画像 URL などのプッシュ通知パラメーターで情報を送信できることは知っています。Facebook は通知領域にメッセージと共にプロフィール写真をどのように表示しますか? 通知領域に外部画像を使いたいので、プルダウンするとプロフィール画像とメッセージが表示されます。現在、ドローアブル フォルダーのデフォルト アイコンのみが表示されます。これはよくある質問かもしれないと思いましたが、何も見つかりませんでした。どんな助けでもいいでしょう。

4

5 に答える 5

36

このステートメントは、URL (当然のことながら、画像を指す URL) を に変換するメソッドを使用しますBitmap

Bitmap bitmap = getBitmapFromURL("https://graph.facebook.com/YOUR_USER_ID/picture?type=large");

注: Facebook のプロフィールについて言及したので、Facebook ユーザーの大きなサイズのプロフィール写真を取得する URL を含めました。ただし、これを に表示する必要がある画像を指す任意の URL に変更できますNotification

そして、上記のステートメントで指定した URL から画像を取得するメソッド:

public Bitmap getBitmapFromURL(String strURL) {
    try {
        URL url = new URL(strURL);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;
    } catch (IOException e) {
        e.printStackTrace();
        return null;
    }
}

bitmap上記で作成したインスタンスをインスタンスに渡しNotification.Builderます。このコード例ではそれを呼び出しbuilderます。次の行で使用されますbuilder.setLargeIcon(bitmap);。実際の構成とその構成を表示する方法を知っていると思いNotificationます。したがって、その部分をスキップして、builderだけを追加します。

// CONSTRUCT THE NOTIFICATION DETAILS
builder.setAutoCancel(true);
builder.setSmallIcon(R.drawable.ic_launcher);
builder.setContentTitle("Some Title");
builder.setContentText("Some Content Text");
builder.setLargeIcon(bitmap);
builder.setContentIntent(pendingIntent);

ああ、ほとんど忘れていましたが、まだ行っていない場合は、マニフェストで次の権限設定が必要になります。

<uses-permission android:name="android.permission.INTERNET" />
于 2013-04-15T04:30:15.983 に答える
18

ここに画像の説明を入力

以下のコードを使用して、最初に画像をダウンロードします。

private Bitmap getBitmap(String url)
    {
        File f=fileCache.getFile(url);

        //from SD cache
        Bitmap b = decodeFile(f);
        if(b!=null)
            return b;

        //from web
        try {
            Bitmap bitmap=null;
            URL imageUrl = new URL(url);
            HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection();
            conn.setConnectTimeout(30000);
            conn.setReadTimeout(30000);
            conn.setInstanceFollowRedirects(true);
            InputStream is=conn.getInputStream();
            OutputStream os = new FileOutputStream(f);
            Utils.CopyStream(is, os);
            os.close();
            bitmap = decodeFile(f);
            return bitmap;
        } catch (Exception ex){
           ex.printStackTrace();
           return null;
        }
    }

    //decodes image and scales it to reduce memory consumption
    private Bitmap decodeFile(File f){
        try {
            //decode image size
            BitmapFactory.Options o = new BitmapFactory.Options();
            o.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(new FileInputStream(f),null,o);

            //Find the correct scale value. It should be the power of 2.
            final int REQUIRED_SIZE=70;
            int width_tmp=o.outWidth, height_tmp=o.outHeight;
            int scale=1;
            while(true){
                if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
                    break;
                width_tmp/=2;
                height_tmp/=2;
                scale*=2;
            }

            //decode with inSampleSize
            BitmapFactory.Options o2 = new BitmapFactory.Options();
            o2.inSampleSize=scale;
            return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
        } catch (FileNotFoundException e) {}
        return null;
    }

以下のコードでその画像をビットマップとして使用します。

Bitmap icon1 = downloadedBitmap;

            NotificationCompat.Builder mBuilder = new NotificationCompat.Builder(
                    this).setAutoCancel(true)
                    .setContentTitle("DJ-Android notification")
                    .setSmallIcon(R.drawable.ic_launcher)
                    .setContentText("Hello World!");

            NotificationCompat.BigPictureStyle bigPicStyle = new NotificationCompat.BigPictureStyle();
            bigPicStyle.bigPicture(icon1);
            bigPicStyle.setBigContentTitle("Dhaval Sodha Parmar");
            mBuilder.setStyle(bigPicStyle);

            // Creates an explicit intent for an Activity in your app
            Intent resultIntent = new Intent(this, testActivity.class);

            // The stack builder object will contain an artificial back stack
            // for
            // the
            // started Activity.
            // This ensures that navigating backward from the Activity leads out
            // of
            // your application to the Home screen.
            TaskStackBuilder stackBuilder = TaskStackBuilder.create(this);

            // Adds the back stack for the Intent (but not the Intent itself)
            stackBuilder.addParentStack(testActivity.class);

            // Adds the Intent that starts the Activity to the top of the stack
            stackBuilder.addNextIntent(resultIntent);
            PendingIntent resultPendingIntent = stackBuilder.getPendingIntent(
                    0, PendingIntent.FLAG_UPDATE_CURRENT);
            mBuilder.setContentIntent(resultPendingIntent);

            NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);

            // mId allows you to update the notification later on.
            mNotificationManager.notify(100, mBuilder.build());

私はあなたがAndroidの許可について知らないことを知っています:

<uses-permission android:name="android.permission.INTERNET" />

詳細については、このアーティカルおよびアンドロイド開発者を確認してください

于 2013-04-15T05:38:39.627 に答える
0

画像はインターネットから読み込まれるため、バックグラウンド スレッドで非同期に実行する必要があります。(1) 非同期タスクまたは (2) Glide (効率的な画像読み込み用) を使用できます。

画像通知をロードするには、「 NotificationCompat.BigPictureStyle() 」を使用する必要があります。これにはビットマップが必要です (画像の URL から抽出する必要があります)。

Glideのほとんどの API とメソッドは非推奨になりました。以下は、Glide 4.9 および Android 10 までで動作します。

ステップ 1: バックグラウンド スレッドで画像 URL からビットマップをロードする

   private void getBitmapAsyncAndDoWork(String imageUrl) {

            final Bitmap[] bitmap = {null};

            Glide.with(getApplicationContext())
                    .asBitmap()
                    .load(imageUrl)
                    .into(new CustomTarget<Bitmap>() {
                        @Override
                        public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {

                            bitmap[0] = resource;
                            // TODO Do some work: Load image notification from here
                            displayImageNotification(bitmap[0]);
                        }

                        @Override
                        public void onLoadCleared(@Nullable Drawable placeholder) {
                        }
                    });
        }

ステップ 2: 画像通知を 1 回表示すると、ビットマップの準備が整います。

private void displayImageNotification(Bitmap bitmap) {

      NotificationCompat.Builder builder = new NotificationCompat.Builder(getApplicationContext(), getChannelId());
            builder
                    .setContentTitle(title)
                    .setContentText(subtext)
                    .setDefaults(Notification.DEFAULT_LIGHTS | Notification.DEFAULT_VIBRATE)
                    .setSmallIcon(SMALL_ICON)
                    .setPriority(NotificationCompat.PRIORITY_DEFAULT)
                    .setColor(getApplicationContext().getColor(color))
                    .setAutoCancel(true)
                    .setOngoing(false)
                    .setOnlyAlertOnce(true)
                    .setContentIntent(pendingIntent)
                     .setStyle(
                     new NotificationCompat.BigPictureStyle().bigPicture(bitmap))
                    .setPriority(Notification.PRIORITY_HIGH);

        getManager().notify(tag, id, builder.build());
}
于 2019-11-08T06:57:15.370 に答える
0

これを解決するためにUniversal Image Loaderを使用しました。設定方法はwikiを参照してください。一度インスタンス化したら、これが GCM リスナーで使用するコードで、画像をダウンロードして表示します。ビットマップをダウンロードして、通知に設定します。

// Download profile picture of the user with Universal Image Loader
Bitmap bitmap =  ImageLoader.getInstance().loadImageSync(profilePhotoUrl);

intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 /* Request code */, intent,
        PendingIntent.FLAG_ONE_SHOT);

Uri defaultSoundUri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this)
        .setSmallIcon(R.drawable.ic_my_app)
        .setLargeIcon(bitmap) // This is the image displayed on the lock screen
        .setContentTitle("My App")
        .setContentText(message)
        .setAutoCancel(true)
        .setSound(defaultSoundUri)
        .setContentIntent(pendingIntent);
于 2016-03-11T01:40:31.717 に答える
-3
Notification notif = new Notification.Builder(context)
     .setContentTitle("Title")
     .setContentText("content")
     .setSmallIcon(R.drawable.ic_small)
     .setLargeIcon(bitmap)
     .setStyle(new Notification.BigPictureStyle()
         .bigPicture(bigBitmap)
         .setBigContentTitle("big title"))
     .build();
于 2015-02-03T08:48:52.177 に答える