samedi 17 avril 2021

getting the same bitmap from random method

in my app the user gets a notification every day with random image , then the user can send that image to whatsapp so i did this

notifyService class :

    public class notifyService extends BroadcastReceiver {
    public static final String CHANNEL_ID ="1";
    @Override
    public void onReceive(Context context, Intent intent) {

    Uri uri = RingtoneManager.getDefaultUri(RingtoneManager.TYPE_NOTIFICATION);

    Intent alarmIntent = new Intent(context, whatsapp_receiver.class);
    PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0, alarmIntent, 0);

    Intent i = new Intent(context, t_receiver.class);
    PendingIntent pending = PendingIntent.getBroadcast(context, 0, i, 0);

 //   Bitmap bitmap = BitmapFactory.decodeResource(context.getResources(), notifyData.notifyMe());

    NotificationCompat.Builder builder = new NotificationCompat.Builder(context, CHANNEL_ID)
            .setVisibility(NotificationCompat.VISIBILITY_PUBLIC)
            .setSmallIcon(R.mipmap.ic_launcher)
            .setSound(uri)
            .addAction(R.drawable.send_w_foreground , "WhatsApp" , pendingIntent)
            .setLargeIcon(new myUri().me(context))
            .setStyle(new NotificationCompat.BigPictureStyle()
                    .bigPicture(new myUri().me(context))
            .setPriority(NotificationCompat.PRIORITY_MAX);
    NotificationManagerCompat.from(context).notify(0 ,builder.build());
    }
    }

whatsapp_receiver class:

    public class whatsapp_receiver extends BroadcastReceiver {
    Uri uri;
    @Override
    public void onReceive(Context context, Intent intent) {
    saveImageExternal(context);
    Intent sendIntent = new Intent();
    sendIntent.setAction(Intent.ACTION_SEND);
    sendIntent.setType("image/*");
    sendIntent.putExtra(Intent.EXTRA_STREAM , uri);
    sendIntent.putExtra(Intent.EXTRA_TEXT , "www.twitter.com");
    sendIntent.setPackage("com.whatsapp");
    sendIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    context.startActivity(sendIntent);
    }
    private void saveImageExternal(Context context ){

    uri = null;
    try {
        File file = new File(context.getExternalFilesDir(Environment.DIRECTORY_PICTURES) , "to-share.png");
        FileOutputStream stream = new FileOutputStream(file);
        new myUri().me(context).compress(Bitmap.CompressFormat.PNG , 90 , stream);
      //  bitmap.compress(Bitmap.CompressFormat.PNG , 90 , stream);
        stream.close();
        uri =Uri.fromFile(file);
    }catch (IOException e){
    }
}

}

myUri class :

    public class myUri {
    public Bitmap me(Context context){
    Bitmap my_bitmap = BitmapFactory.decodeResource(context.getResources() ,notifyData.notifyMe());
    return my_bitmap;
    }
    }

notifyData class :

    `public class notifyData {
     public static int notifyMe() {
    int[] notification = {
            R.drawable.yellow,R.drawable.lue,R.drawable.green,R.drawable.red
    };
    int idx = new Random().nextInt(notification.length);
    int random = (notification[idx]);
    return random;
    }
    }

problem is : i want to make the image received with notifications , the same one that the user can send to whatsapp !

i hope someone give me the answer

thank you in advance




Aucun commentaire:

Enregistrer un commentaire