I have a notification listener service that reads notifications from other apps (with the user's permission) and extracts all the data. Able to access everything except the image shown in the expanded view of the notification.
I am also reading the EXTRA_PICTURE intent value
if (extras.containsKey(Notification.EXTRA_PICTURE)) {
// this bitmap contain the picture attachment
try {
Bitmap bmp = (Bitmap) extras.get(Notification.EXTRA_PICTURE);
ByteArrayOutputStream picStream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, picStream);
byte[] picBmpData = picStream.toByteArray();
notificationPicture = Base64.encodeToString(picBmpData, Base64.NO_WRAP);
} catch(Exception e){
e.printStackTrace();
}
}
This works sometimes but for all notifications with image in their expanded view. Am I missing anything?
UPDATE: more clarification on what happens in the negative cases:
In negative cases when there is an image in the notification, the extras dont seem to have the EXTRA_PICTURE key set. However I do see
android.template
key set to
android.app.Notification$BigPictureStyle
Related
Good morning, I'm using this Github library: https://github.com/burhanrashid52/PhotoEditor as photo editor. I have a GalleryFragment in which user choose image from his gallery, the I pass correctly the image to the EditImageActivity where user can apply sticker, filters, ecc. and then clicking on a button, user can pass the edited image to InfoActivity, where he can add other info and publish the image.
The problem is that when user edits an image and pass it to InfoActivity it is shown the original image without changes, and also when he publishes it, the image saved is the original, not the modified.
I'm trying to use Bitmap to do that:
This is the code of EditImageActivity where I try to send the edited image in a intent to InfoActivity:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
Bitmap bmp =((BitmapDrawable)mPhotoEditorView.getSource().getDrawable()).getBitmap();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Intent intent = new Intent(EditImageActivity.this, InfoActivity.class);
intent.putExtra("imm", byteArray);
startActivity(intent);
And this is the piece of code of InfoActivity, where I try to retrieve the intent with the edited image from EditImageActivity, to show it in a ImageView:
byte[] byteArray = getIntent().getByteArrayExtra("imm");
assert byteArray != null;
Bitmap bmp = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
immagine.setImageBitmap(bmp);
So it works when user doesn't edit image, while when he edits image, it shows the original.
I think that the problem is in this code (the code that I use in EditImageActivity to get a bitmap of the edited image), because it passes the original image instead of the modified image:
Bitmap bmp =((BitmapDrawable)mPhotoEditorView.getSource().getDrawable()).getBitmap();
Can someone help me please?
Yes, it seems that the problem is when you fetch the edited image from your PhotoEditorView. If you see the documentation, to retrieve this image, is necessary to implement the next approach:
PhotoEditor.saveAsFile(filePath, new PhotoEditor.OnSaveListener() {
#Override
public void onSuccess(#NonNull String imagePath) {
Log.e("PhotoEditor","Image Saved Successfully");
}
#Override
public void onFailure(#NonNull Exception exception) {
Log.e("PhotoEditor","Failed to save Image");
}
});
So the library needs a file to save it and if is success, then returns the path file, with the path file you needs to create the Bitmap object.
I'm trying to make a little Android app that triggers a screenshot capture of the current showing screen when a specific notification appears. For example, I'm on whatsapp and a Whatsapp notification appears -> That triggers a whatsapp capture.
Well, my current code actually detects notifications and triggers a screenshot when a notification comes, but not the way I want. I get a screenshot of my MainActivity, even if it's not showing on the screen. I just want to screenshot what it's appearing on the screen. It seems easy but I'm not being able to do it!
I leave my current NotificationReceiver class, which is failing because it captures the MainActivity and not the screen:
class NotificationReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context context, Intent intent) {
String temp = intent.getStringExtra("notification_event") + "\n" + txtView.getText();
txtView.setText(temp);
if (intent.getStringExtra("notification_event").contains("bet")) {
Log.i("Dentro", "dentro");
Date now = new Date();
android.text.format.DateFormat.format("yyyy-MM-dd_hh:mm:ss", now);
try {
// image naming and path to include sd card appending name you choose for file
String mPath = Environment.getExternalStorageDirectory().toString() + "/" + now + ".jpg";
// create bitmap screen capture
View v1 = getWindow().getDecorView().getRootView();
v1.setDrawingCacheEnabled(true);
Bitmap bitmap = Bitmap.createBitmap(v1.getDrawingCache());
v1.setDrawingCacheEnabled(false);
File imageFile = new File(mPath);
FileOutputStream outputStream = new FileOutputStream(imageFile);
int quality = 100;
bitmap.compress(Bitmap.CompressFormat.JPEG, quality, outputStream);
outputStream.flush();
outputStream.close();
} catch (Throwable e) {
// Several error may come out with file handling or OOM
Log.i("Catch","Error dentro del if(contains)");
e.printStackTrace();
}
}//fin if
}
}
Any ideas of how can I proceed? I'm actually stuck. Help would be really appreciated!
You would need to use the media projection APIs on Android 5.0+, and set your minSdkVersion to 21. For privacy and security reasons, apps cannot take screenshots of other apps without explicit user permission, and that only became possible with Android 5.0.
This sample app demonstrates taking screenshots on demand.
I am integrating my Android app with Layer chat sdk (https://layer.com/).
I am going through the Layer Documentation for implementing the chat, I had succeed, I can able to send, receive the text messages.
My problem is when I am sending an image successfully, but receiver not able to receiving the image
Sending Image code:
Bitmap imageBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.back_icon);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
imageBitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] imageData = stream.toByteArray();
MessagePart messagePart = layerClient.newMessagePart("image/jpeg", imageData);
Receiving Image code:
{
List<MessagePart> parts = message.getMessageParts();
for(MessagePart part : parts) {
switch (part.getMimeType()) {
case "text/plain":
String textMsg = new String(part.getData());
break;
case "image/jpeg":
Bitmap imageMsg = BitmapFactory.decodeByteArray(part.getData(), 0, part.getData().length);
break;
}
}
Why is here part.getData() value returning null?
layerClient.setAutoDownloadMimeTypes(Arrays.asList("image/jpeg"));
The Above code will take by default less than 2KB if the size is exceeds
layerClient.setAutoDownloadSizeThreshold(1024 * 100);
You need to specify this code it will take up to 100kb,Your problem will solve
for more info : https://developer.layer.com/docs/android/guides#richcontent
My app has a feature that posts a photo + text to a user's facebook wall (works fine). Now I'm trying to include a link in the text that goes to a specific facebook page (doesn't work).
The basic code looks like this (works fine):
private void postImageToFacebookWall(String filePath, String msg) {
try {
Bundle param = new Bundle();
param = new Bundle();
// prep photo byte array
Bitmap bitmap = BitmapFactory.decodeFile(filePath);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
// add byte array and user msg
param.putByteArray("image", byteArray);
param.putString("message", msg);
// post to Facebook
mAsyncRunner.request("me/photos", param, "POST", new PostRequestListener(), null);
} catch (Exception e) {
e.printStackTrace();
}
}
Now I'm trying to embed a link to a facebook page in the msg, using the following syntax:
#[fb_page_id:str]
This works when I type it directly into facebook. But it doesn't work when I use it in the code, modified as follows (doesn't work):
String fbPageRef = "#[" + Constants.FACEBOOK_PAGE_ID + ":str]";
param.putString("message", msg + " " + fbPageRef);
When I run the code with the embedded link (fbPageRef), it doesn't show up.
What am I doing wrong? Thanks.
I didn't notice at first that a facebook post generated by an app already includes an attribution to the source app. It's at the bottom of the post and reads something like: "2 hours ago via YourAppName".
This is friendly to read, and correctly links back to the app's facebook page (assuming there is one). So if that's good enough, you don't have to worry about how to insert a link in the text part of the post!
Still it would be good to know how to embed a link to a facebook page in a machine-posted message (?). Thanks.
I'm trying to share from my app to Hyves using share intent. If I have hyves app installed and share from gallery it switch to hyves app, and upload image correctly to hyves, so it should work.
Problem is, I can't find it documented how should proper intent for hyves work, but I assume that gallery uploads images only, so I have this:
Bitmap image = BitmapFactory.decodeFile(MyGlobals.INSTANCE.activeSetting.f_image_path);
It's line of code where I pull my "active" or "selected" image within my app. At this point image is saved to SD Card, so I might read uri instead of decoding file, but I want it this way to have same approach for both hyves and facebook.
Then I call:
Intent hyvesIntent = new Intent(Intent.ACTION_SEND);
hyvesIntent.setPackage("com.hyves.android.application");
hyvesIntent.setType("image/jpeg");
hyvesIntent.putExtra("image", image);
startActivityForResult(hyvesIntent, 666);
First of all, I'm not sure if setPackage is OK to be used here, but I'm checking if this package exist to enable / disable share, and this is package name that is visible.
I need Activity result to then notify that Image is shared or not.
What happens here it starts the Hyves app, but I get full white screen, and then the Hyves app times out.
So, Can I use Bitmap in intent, and is it OK to setPackage or should I setClass?
Tnx
Maybe you can not put the bitmap directly in the intent.
First convert the bitmap into a byte array than send another side and convert into bitmap
//convert bitmap to bytearray
public static byte[] getBitmapAsByteArray(Bitmap bitmap, boolean type) {
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
if (type) {
bitmap.compress(CompressFormat.PNG, 0, outputStream);
} else {
bitmap.compress(CompressFormat.JPEG, 0, outputStream);
}
return outputStream.toByteArray();
}
//convert bitmap to bytearray
public static Bitmap getBitmap(byte[] imgByte){
Bitmap bm = BitmapFactory.decodeByteArray(imgByte, 0, imgByte.length);
return bm;
}
//than intent
Intent hyvesIntent = new Intent(Intent.ACTION_SEND);
hyvesIntent.setPackage("com.hyves.android.application");
hyvesIntent.setType("image/jpeg");
hyvesIntent.putExtra("image", getBitmapAsByteArray(image,true));
startActivityForResult(hyvesIntent, 666);
I hope that is right way to put image