I showed a photo on the map using the osmdroid and first methods.
Now I want to read a jpeg format picture from the device memory and show it as a bitmap on the map. The code does not give an error. But the photo is not displayed. please guide me. The following are the first and second method codes.
/// first method
GroundOverlay myGroundOverlay = new GroundOverlay();
myGroundOverlay.setPosition(overlayCenterPoint);
Drawable d = ResourcesCompat.getDrawable(getResources(), R.mipmap.ic_imageonmap_test2, null);
myGroundOverlay.setImage(d.mutate());
//myGroundOverlay.setImageBitmap(bitmap);
myGroundOverlay.setDimensions(2000000.0f);
myGroundOverlay.setTransparency(0.25f);
myGroundOverlay.setBearing(0);
mMapView.getOverlays().add(myGroundOverlay);
mMapView.invalidate();
////// second method
Bitmap src = BitmapFactory.decodeFile("/sdcard/osmdroid/20190722033213_1440103389.jpg");
Bitmap bitmap2 = src.copy(Bitmap.Config.ARGB_8888, true);
GroundOverlay myGroundOverlay2 = new GroundOverlay();
myGroundOverlay2.setPosition(overlayCenterPoint);
//myGroundOverlay2.setImageBitmap(src);
myGroundOverlay2.setImageBitmap(bitmap2);
//myGroundOverlay2.setDimensions(2000.0f);
//myGroundOverlay2.setTransparency(0.25f);
myGroundOverlay2.setBearing(0);
mMapView.getOverlays().add(myGroundOverlay2);
mMapView.invalidate();
I assume you are using OSMBonusPack GroundOverlay.
Your code in method 1 seems correct (even if dimension seems huge).
In method 2, setDimensions is missing.
Did you try the OSMBonusPack tutorial "as is" (same position/dimension, same bitmap from resources)?
Also, try: "setBearing(0.0f);" (I'm not confident about implicit cast from int to float)
Related
I am trying to use GroundOverlayOptions to add a custom image to my Android.gms.maps
BitmapDescriptor image = BitmapDescriptorFactory.FromPath("fire2.bmp");
GroundOverlayOptions newarkMap = new
GroundOverlayOptions().InvokeImage(image);
newarkMap.Position(e.Point, 8600f, 6500f);
map.AddGroundOverlay(newarkMap);
Despite fire2.bmp being a bitmap, I am still receiving the error:
Java.Lang.IllegalArgumentException: Failed to decode image. The provided image must be a Bitmap.
What am I missing?
I suggest to use BitmapDescriptorFactory.FromResource. If at compile time you don't get any errors that means your resource exists and will be found at runtime
Is there any good way to use frame by frame animation AnimationDrawable using UIL library. AnimationDrawable accept only BitmpaDrawable . Is there any way to convert quickly to bitmaps my images or any maybe there is method like imageLoader.getBitmap, I haven't find anything like that.
Please help , I would be very grateful for any help .
Edit I have my images in assets folder. Maybe there any way to get bitmap from cache or something else . I need open new activity , maybe several times . I need to show animation from files , but if I use decode it takes a lot of time to decode them . Please suggest something
If your images are resources you can use this for obtain Bitmap:
Bitmap icon = BitmapFactory.decodeResource(context.getResources(),
R.drawable.icon_resource);
else, if you want donwload the image you can use this:
String name = c.getString(url);
URL urlAux = new URL(name);
ImageView prueba = (ImageView)v.findViewById(R.id.iv_prueba);
if (prueba != null) {
Bitmap bitmap =
BitmapFactory.decodeStream(urlAux.openConnection().getInputStream());
prueba.setImageBitmap(bitmap);
}
Hope it helps you.
I tried searching for a relevant example but found none, so I'll try to be as precise as I can.
Back in the gingerbread era I made a draw bitmap to canvas code that worked flawlessly and here it is.
//this one is drawing the background for my picture
mBackground = BitmapFactory.decodeResource(getResources(), R.drawable.base);
//setting a new canvas
mComboImage = new Canvas(mBackground);
//adding another bitmap to the canvas, don't worry about the size or the i variables
mBackImage = BitmapFactory.decodeResource(getResources(), R.drawable.base);
mBackImage=Bitmap.createScaledBitmap(mBackImage, size, 105, false);
mComboImage.drawBitmap(mBackImage, 100f+1f*size*i, 170f, null);
mBackImage.recycle();
//this is setting my background for an icon to the completed image
Drawable d =new BitmapDrawable(getResources(),mBackground);
aaa.setBackground(d);
anyways the code doesn't seem to fit now. One problem I have faced is converting the bitmap into mutable which you can check out here if you are stuck on it as I was for a while.
My problem is that the background is perfectly drawn but the mBackImage doesn't show up at all.
What worries me more is that this used to work perfectly before.
I really have tried searching for a newer explanation but haven't really found any on stackoverflow so
I drew bitmap as background image for my app as well. I used: setBackgroundDrawable() instead of setBackground() in your last statement.
Also, when preparing the bitmap, I set two options:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPurgeable = true;
options.inInputShareable=true;
bmp = BitmapFactory.decodeResource(context.getResources(), R.drawable.image1), options);
You can give it a try.
I used the code from this answer, and compiled everything successfully.
However, I cannot find out how to use the drawable that I have just created. Here is my code:
Drawable dr2 = getResources().getDrawable(android.R.drawable.ic_menu_manage);
Bitmap bitmap2 = ((BitmapDrawable) dr2).getBitmap();
Drawable f = new BitmapDrawable(getResources(), Bitmap.createScaledBitmap(bitmap2, 256, 256, true));
private Integer[] menu_icon = {
android.R.drawable.f,
// I can't figure out how to call Drawable f
};
After converting the drawable to the Drawable f, I am unable to find out how to recall the resized drawable. I have tried using all possible locations in my code as to where it may be (/res/drawable and a few other locations). However, I am unable to compile no matter how I try to call Drawable f. Any help would be greatly appreciated!
(And no, I have eight elements in that array, but only copy+pasted one)
It exists only in memory, not anywhere on disk. You can't reference it by id. Instead, you need to call versions of functions that take a Drawable rather than ones that take an int resourceID.
I was trying to set a bitmap image to a canvas using setBitMap ,at that time I got an IllegalStateException.This canvas have some images on it currently, I am trying to replace it.
Any one have any idea why this happened?
Code Snippet
editBm = Bitmap.createBitmap(951, 552, Bitmap.Config.ARGB_8888);
Canvas mCanvas=new Canvas(editBm);
eBit=LoadBMPsdcard(filePath); ---->returns a bitmap when the file path to the file is provided
Log.i("BM size", editBm.getWidth()+"");
mCanvas.setBitmap(eBit);
I am not getting any NullPointer errors and the method LoadBMPsdcard() is working good.
Please let me know about any ideas you have ...
Thanks in advance
Happy Coding
IllegalStateException could be thrown because you're loading a Bitmap (eBit) and use mCanvas.setBitmap(eBit) without checking if the bitmap is mutable. This is requiered to draw on the Bitmap. To make sure your Bitmap is mutable use:
eBit=LoadBMPsdcard(filePath);
Bitmap bitmap = eBit.copy(Bitmap.Config.ARGB_8888, true);
canvas.setBitmap(bitmap);
Try to use drawBitmap instead of the setBitmap. It looks like you've already set a bitmap to draw into by passing it to the canvas constructor, so now you just need to draw everything onto it.
Canvas.setBitmap() throws IllegalStateException if and only if Bitmap.isMutable() returns true. Bitmap.createBitmap() builds an immutable Bitmap instance only, in all of its forms. To create a mutable bitmap you either use new Bitmap(), or Bitmap.copy(true), depending on whether you have a source bitmap that you want to start with. A typical block for me looks like:
Bitmap image = ...
Canvas c = new Canvas(image.isMutable()?image:image.copy(true));
...
This assumes, of course, that you don't mind clobbering the source Bitmap (which I generally don't but that's by no means universal).