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
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 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
I'm developing a simple android game divided into levels. I want a check icon (ImageView) to appear next to a level button (on level select menu) when that level is completed.
A level is completed after pressing a button, as follows (InsideLevelActivity):
final EditText level1editText=(EditText)findViewById(R.id.level1editText);
Button level1completeButton=(Button)findViewById(R.id.level1completeButton);
level1completeButton.setOnClickListener(new View.OnClickListener()
public void onClick(View v)
{
final String edittext=level1editText.getText().toString();
if(edittext.trim().equals("Complete level"))
{
{
Intent visible1 = new Intent();
visible1.putExtra("iconvisible",0);
startActivity(visible1);
{
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.activity_level1completed,
(ViewGroup) findViewById(R.id.img11_toast));
Toast toast = new Toast(Level1Activity.this);
toast.setView(view);
toast.show();
{ onBackPressed(); {
return;
}
}
}
else
{
Toast.makeText(Level1Activity.this,
"Type Complete level.", Toast.LENGTH_LONG).show();
}
And then returns to level select menu activity. I'm trying to retrieve data this way (LevelMenuActivity):
ImageView logocheckicon1=(ImageView)findViewById(R.id.logocheckicon1);
logocheckicon1.setVisibility(View.GONE);
Intent visible1 = getIntent();
int state = Integer.parseInt(visible1.getExtras().get("iconvisible").toString());
complete1.setVisibility(iconvisible);
I've tried many approaches for the last couple of days, including this one (how to pass data between two activities). I've even tried to make the check icon (ImageView) invisible, and making it visible again this way.
Also, the same check icon will appear next to every completed level. Is it possible to acomplish this with only one ImageView (without creating 10 different IDs of the same drawable)?
Thank you in advance.
EDIT:
I apologize if i wasn't clear enough. I tought there was some way to change the visibility of an image located, for instance, on MainActivity with an intent inside the button on another activity.
Thank you for your answers.
EDIT2: Added the code of another unsuccessful try.
To Pass image from one activity to another activity. At First convert image into Bitmap then base64 then convert string then pass it via intent or save share-preference.
public boolean saveImage(Context context, Bitmap realImage) {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
realImage.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
}
Then take this image in another activity via intent or get from share-preference
public Bitmap getFacebookImageBitmap(Context context)
{
Bitmap bitmap = null;
String saveimage=from intent or share-preference string.
if( !saveimage.equalsIgnoreCase("") ){
byte[] b = Base64.decode(saveimage, Base64.DEFAULT);
bitmap = BitmapFactory.decodeByteArray(b, 0, b.length);
}
return bitmap;
}
Thanks
You can pass the image through intents. First convert your image to a byte array and send it with the intent.
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, baos);
byte[] b = baos.toByteArray();
Intent intent = new Intent(this, ActivityB.class);
intent.putExtra("picture", b);
startActivity(intent);
then you can retrieve this image from the next avtivity.
Bundle extras = getIntent().getExtras();
byte[] b = extras.getByteArray("picture");
Bitmap bmp = BitmapFactory.decodeByteArray(b, 0, b.length);
ImageView image = (ImageView) findViewById(R.id.imageView1);
image.setImageBitmap(bmp);
Hope this will help you.
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
I want to pass the Background Image that I have set to Button in PutExtra() with intent object into another Class.
Can anybody know how to do that ?
Thanks
davidbrown
Sender Activity:
Bitmap bitmap = BitmapFactory.decodeResource
(getResources(), R.drawable.sticky_notes); // your bitmap
ByteArrayOutputStream bs = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 50, bs);
intent.putExtra("byteArray", bs.toByteArray());
Reciever Activity:
if(getIntent().hasExtra("byteArray")) {
ImageView imv= new ImageView(this);
Bitmap bitmap = BitmapFactory.decodeByteArray(
getIntent().getByteArrayExtra("byteArray"), 0, getIntent().getByteArrayExtra("byteArray").length);
imv.setImageBitmap(bitmap);
}
Intent can keep only 40 kbytes. If you can zip your images less then 40 kbytes - you can put it into extras
intent.putExtra("imageData", bitmap)
better approach is to create a link instead of passing directly bitmap.
intent.putExtra("image_url",R.drawable.image);
try this...
first get image in bitmap.
Bitmap tileImage = BitmapFactory.decodeResource(getResources(), R.drawable.floore);
Conver it in byte array.
ByteArrayOutputStream stream = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Bundle b = new Bundle();
b.putByteArray("camara",byteArray);
Intent intent3 = new Intent(this,Second.class);
intent3.putExtras(b);
startActivity(intent3);
You can pass Bitmap (since it's implementing Parcelable) if you're sure that it won't be deleted from memory (in other words - don't store Bitmaps like that).
Bitmap itself is just a small Java wrapper of native resources, so it won't take a lot of space.
Be careful when passing files that can be very large, such as a photo or gallery file. Even if you compress it, the size may exceed putExtra's acceptable limit. I suggest sending the image link or file path for a file from the gallery. In my app I always compress my files as max as I can, but there was always one that crashs the app.
Intent intent = new Intent(getActivity(), PhotoViewActivity.class);
intent.putExtra("url", url);
//OR file path
intent.putExtra("path", path);
startActivityForResult(intent,PHOTO_VIEW_REQUEST);
In that case on PhotoViewActivity
String url = getIntent().getStringExtra("url");
String path = getIntent().getStringExtra("path");
if(url != null && !url.isEmpty()){
//Get using Picasso or other framework
}else if(path != null && !path.isEmpty()){
//In my case I transform in Bitmap
//see this link for more detail : https://stackoverflow.com/questions/16804404/create-a-bitmap-drawable-from-file-path
}else{
//Throw exception and close activity
}
How transform path in Bitmap:
Create a Bitmap/Drawable from file path