hey I customize google map makers as an image in my android app , it's working fine but currently it takes time to load markers since there are many markers around 200. Actually, i get the images from backend (parse.com) and then crop it to fit marker shape.
Is there any way to load markers faster?
View marker2 = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.marker, null);
ImageView numTxt = (ImageView) marker2.findViewById(R.id.num_txt);
Bitmap bitmap1 = getBitmap(url1, MapActivity.this);
numTxt.setImageBitmap(bitmap1);
markerOpts = markerOpts.title("Company").snippet("branch").icon(BitmapDescriptorFactory.fromBitmap(createDrawableFromView(MapActivity.this, marker2)));
public static Bitmap getBitmap(String url2, Context context) {
FileCache fileCache = new FileCache(context);
MemoryCache memoryCache = new MemoryCache();
File f = fileCache.getFile(url2);
//from SD cache
//CHECK : if trying to decode file which not exist in cache return null
Bitmap b = decodeFile(f);
if (b != null)
return b;
// Download image file from web
try {
Bitmap bitmap = null;
URL imageUrl = new URL(url2);
HttpURLConnection conn = (HttpURLConnection) imageUrl.openConnection();
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setInstanceFollowRedirects(true);
InputStream is = conn.getInputStream();
// Constructs a new FileOutputStream that writes to file
// if file not exist then it will create file
OutputStream os = new FileOutputStream(f);
// See Utils class CopyStream method
// It will each pixel from input stream and
// write pixels to output stream (file)
Utils.CopyStream(is, os);
os.close();
conn.disconnect();
//Now file created and going to resize file with defined height
// Decodes image and scales it to reduce memory consumption
b = decodeFile(f);
return bitmap;
} catch (Throwable ex) {
ex.printStackTrace();
if (ex instanceof OutOfMemoryError)
memoryCache.clear();
return null;
}
}
public static Bitmap createDrawableFromView(Context context, View view) {
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
view.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
view.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
view.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
view.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(view.getMeasuredWidth(), view.getMeasuredHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
view.draw(canvas);
return bitmap;
}
Yes, there is. You know for sure that you will need the pictures, so do that in an un-obtrusive way. Load them in the background once, while initializing. After you have loaded them once, just use them from your local environment. So, improvements:
request the pictures and parse them in a background thread while initializing the software to not bother the user with it
store the pictures when you have successfully extracted them and reuse them locally instead of rerequesting them
You can use image downloading library Picasso.
It will handle image downloading and caching, memory usage, and you can crop an image to whatever size you need. And if you load the same image many times (what you probably do) Picasso will use cached image instead of downloading it every time, so that will speed up all the images downloading process.
To use this library you need to declare it in dependency tree of build.gradle file from app folder of your project in Android Studio, so it will look like this:
dependencies {
//some other dependencies
//...
compile 'com.squareup.picasso:picasso:2.5.2'//add this line
}
Then use it in code:
Picasso.with(MapActivity.this).load(url1).into(numTxt);
Related
I am using Mapbox in my application so I'm displaying customizing marker in the map.I will get image from API as image URL,if I directly pass the url image is not displying so I converted as the Bitmap image,sometimes applciation getting crashed.Please help me.
java.lang.OutOfMemoryError: Failed to allocate a 53934348 byte allocation with 4194208 free bytes and 36MB until OOM
Note:while converting the url into bitmap that time itself application crashed sometimes.
Code:
//converting the url into bitmap
try {
URL url = new URL(“http://192.168.0.109/images/profile/2018-04-03-14-01-022017-11-13-07-12-36Image.jpg”);
Bitmap bitmapimage = BitmapFactory.decodeStream(url.openConnection().getInputStream()); }
catch(IOException e) { System.out.println(e); }
//customizing the marker
MarkerViewOptions markerViewOptions = new MarkerViewOptions()
.position(new LatLng(Double.parseDouble(userDetails.getLatitude()), Double.parseDouble(userDetails.getLongitutde())))
.icon(drawableToIcon(getContext(),bitmapimage, userDetails.getUser_status()));
mMapBoxMap.addMarker(markerViewOptions);
//method to customise the layout
public Icon drawableToIcon(#NonNull Context context, Bitmap image, String status) {
View marker = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.custom_marker_layout, null);
CircleImageView markerImage = (CircleImageView) marker.findViewById(R.id.user_dp);
markerImage.setImageBitmap(image);
DisplayMetrics displayMetrics = new DisplayMetrics();
((Activity) context).getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
marker.setLayoutParams(new ViewGroup.LayoutParams(52, ViewGroup.LayoutParams.WRAP_CONTENT));
marker.measure(displayMetrics.widthPixels, displayMetrics.heightPixels);
marker.layout(0, 0, displayMetrics.widthPixels, displayMetrics.heightPixels);
marker.buildDrawingCache();
Bitmap bitmap = Bitmap.createBitmap(marker.getMeasuredWidth(), marker.getMeasuredHeight(), Bitmap.Config.ARGB_4444);
Canvas canvas = new Canvas(bitmap);
marker.draw(canvas);
return IconFactory.getInstance(context).fromBitmap(bitmap);
}
I referred this link http://madhusudhanrc.blogspot.in/2012/09/reduce-bitmap-size-using.html ,if I shrink the image means its not diplaying.Please help me to reduce the bitmap size.
Hello I am trying to create PDF from webview in Crosswalk but I didn't find any supporting methods how to do it. Currently I am using this option to create bitmap
How can I capture the whole view into a Bitmap when using crosswalk to display webpage?
and then I try to convert the bitmap using itext lib
How I can convert a bitmap into PDF format in android as output I get blank pdf (black page).
Here is the code for bitmap:
private void createBitmap (XWalkView view) {
String pathToFile = Environment.getExternalStorageDirectory() +"/bitmap.jpg";
view.setDrawingCacheEnabled(true);
view.buildDrawingCache(true);
Bitmap btmp = view.getDrawingCache();
Canvas canvas = new Canvas(btmp);
Paint paint = new Paint ();
int height = btmp.getHeight();
canvas.drawBitmap(btmp, 0, height, paint);
view.draw(canvas);
try {
OutputStream fOut = null;
File file = new File(pathToFile);
fOut = new FileOutputStream(file);
btmp.compress(Bitmap.CompressFormat.JPEG, 50, fOut);
fOut.flush();
fOut.close();
btmp.recycle();
view.setDrawingCacheEnabled(false);
view.destroyDrawingCache();
} catch (Exception e) {
e.printStackTrace();
}
}
I need just hint :). Thanks a lot. This is my first question I apologize for typos
XWalkView use a standalone SurfaceView(or TextureView) as its output target.The draw or onDraw of XWalkView draws nothing. So you can not rely on it to draw something into a pdf file.
As a workaround, there is another api XWalkView.captureBitmapAsync which can be used to capture the visible webpage into a bitmap. Here is a simple guide about how to use it:
1), Implement XWalkGetBitmapCallback
import org.xwalk.core.XWalkGetBitmapCallback;
class XWalkGetBitmapCallbackImpl extends XWalkGetBitmapCallback {
public XWalkGetBitmapCallbackImpl() {
super();
}
//Note: onFinishGetBitmap happens at the same thread as captureBitmapAsync, usually the UI thread.
#Override
public void onFinishGetBitmap(Bitmap bitmap, int response) {
//if response == 0, save this bitmap into a jpg file //otherwise errors.
}
}
2), Start the capture in UI thread:
private void captureContent() {
if ( xWalkView == null) return;
mXWalkGetBitmapCallback = new XWalkGetBitmapCallbackImpl();
xWalkView.captureBitmapAsync(mXWalkGetBitmapCallback);
}
More details is here: https://crosswalk-project.org/jira/browse/XWALK-5110
On android I am drawing into a android.graphics.Picture then save the Picture to a file. Later I reload the picture into memory and draw it to the canvas. I noticed that Bitmaps were never drawing. And after much debugging I managed to narrow down the problem to Picture.writeToStream and Picture.createFromStream. It seems that Bitmaps drawn into the picture don't get reloaded properly. Below is sample code I wrote to show the problem. In this sample my canvas is not hardware accelerated.
So my questions are as follows:
Am I doing something wrong?
Is this an Android bug? I filed the bug report https://code.google.com/p/android/issues/detail?id=54896 because I think this is.
Any known workaround?
#Override
protected void onDraw(Canvas canvas)
{
try
{
Picture picture = new Picture();
// Create a bitmap
Bitmap bitmap = Bitmap.createBitmap( 100, 100, Config.ARGB_8888);
Canvas bitmapCanvas = new Canvas(bitmap);
bitmapCanvas.drawARGB(255, 0, 255, 0);
// Draw the bitmap to the picture's canvas.
Canvas pictureCanvas = picture.beginRecording(canvas.getWidth(), canvas.getHeight());
RectF dstRect = new RectF(0, 0, 200, 200);
pictureCanvas.drawBitmap(bitmap, null, dstRect, null);
picture.endRecording();
// Save the Picture to a file.
File file = File.createTempFile("cache", ".pic");
FileOutputStream os = new FileOutputStream(file);
picture.writeToStream(os);
os.close();
// Read the picture back in
FileInputStream in = new FileInputStream(file);
Picture cachedPicture = Picture.createFromStream(in);
// Draw the cached picture to the view's canvas. This won't draw the bitmap!
canvas.drawPicture(cachedPicture);
// Uncomment the following line to see that Drawing the Picture without reloading
// it from disk works fine.
//canvas.drawPicture(picture);
}
catch (Exception e)
{
}
}
I did find an answer to this question after looking at the native android code that backs the Bitmap. Android only can only save certain types of bitmaps to the picture. This is because the SkBitmap class only supports certain types of inputs that result in a bitmap that can be saved to a Picture. So in this can I can workaround the problem by providing those magical inputs. Use a bitmap that is saved to disk and call BitmapFactory.decodeFileDescriptor to create it.
private Bitmap createReusableBitmap(Bitmap inBitmap)
{
Bitmap reuseableBitmap = null;
if (inBitmap== null)
return null;
try
{
// The caller is responsible for deleting the file.
File tmpBitmapFile = File.createTempFile("bitmap", ".png");
setBitmapPath(tmpBitmapFile.getAbsolutePath());
FileOutputStream out = new FileOutputStream(tmpBitmapFile);
boolean compressed = inBitmap.compress(CompressFormat.PNG, 100, out);
out.close();
if (compressed)
{
// Have to create a purgeable bitmap b/c that is the only kind that works right when drawing into a
// Picture. After digging through the android source I found decodeFileDescriptor will create the one we need.
// See https://github.com/android/platform_frameworks_base/blob/master/core/jni/android/graphics/BitmapFactory.cpp
// In short we have to give the options inPurgeable=true inInputShareable=true and call decodeFileDescriptor
BitmapFactory.Options options = new BitmapFactory.Options();
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
options.inInputShareable = true;
options.inPurgeable = true;
options.inSampleSize = 1;
options.inScaled = false;
options.inMutable = false;
options.inTempStorage = DraftRenderer.tempStorage;
FileInputStream inStream = new FileInputStream(tmpBitmapFile);
FileDescriptor fd = inStream.getFD();
reuseableBitmap = BitmapFactory.decodeFileDescriptor(fd, null, options);
inStream.close();
}
} catch (Exception e) {
}
return reuseableBitmap;
}
Note: a picture created from an input stream cannot be replayed on a hardware accelerated canvas.
Picture.createFromStream(InputStream stream)
you can use canvas.isHardwareAccelerated() to detect hardware accelerated or not.
This seems simple, I am trying to set a bitmap image but from the resources, I have within the application in the drawable folder.
bm = BitmapFactory.decodeResource(null, R.id.image);
Is this correct?
Assuming you are calling this in an Activity class
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.image);
The first parameter, Resources, is required. It is normally obtainable in any Context (and subclasses like Activity).
Try this
This is from sdcard
ImageView image = (ImageView) findViewById(R.id.test_image);
Bitmap bMap = BitmapFactory.decodeFile("/sdcard/test2.png");
image.setImageBitmap(bMap);
This is from resources
Bitmap bMap = BitmapFactory.decodeResource(getResources(), R.drawable.icon);
If the resource is showing and is a view, you can also capture it. Like a screenshot:
View rootView = ((View) findViewById(R.id.yourView)).getRootView();
rootView.setDrawingCacheEnabled(true);
rootView.layout(0, 0, rootView.getWidth(), rootView.getHeight());
rootView.buildDrawingCache();
Bitmap bm = Bitmap.createBitmap(rootView.getDrawingCache());
rootView.setDrawingCacheEnabled(false);
This actually grabs the whole layout but you can alter as you wish.
If you have declare a bitmap object and you want to display it or store this bitmap object. but first you have to assign any image , and you may use the button click event, this code will only demonstrate that how to store the drawable image in bitmap Object.
Bitmap contact_pic = BitmapFactory.decodeResource(
v.getContext().getResources(),
R.drawable.android_logo
);
Now you can use this bitmap object, whether you want to store it, or to use it in google maps while drawing a pic on fixed latitude and longitude, or to use some where else
just replace this line
bm = BitmapFactory.decodeResource(null, R.id.image);
with
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.YourImageName);
I mean to say just change null value with getResources() If you use this code in any button or Image view click event just append getApplicationContext() before getResources()..
Using this function you can get Image Bitmap. Just pass image 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;
}
}
I am working on an Android app that displays photos which are downloaded from Flickr. I obtain a bitmap object from a byte array, which in turn is read from the relevant Flickr URL, as follows:
BitmapFactory.Options opt = new BitmapFactory.Options();
opt.inDither = true;
opt.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opt);
I then draw the bitmap onto a canvas in the onDraw method of a View object:
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG | Paint.FILTER_BITMAP_FLAG);
canvas.drawBitmap(bitmap, 0, 0, paint);
The problem is that the resulting picture is pixelated and I can't figure out why; I have tried a number of variations of the opt and paint objects with no luck. The difference between the picture displayed in my app and the picture at the original URL is roughly demonstrated by the following:
Bad image, see pixelation in top left corner http://homepages.inf.ed.ac.uk/s0677975/bad.jpg
Good picture, this is the expected result http://homepages.inf.ed.ac.uk/s0677975/good.jpg
Look e.g. at the clouds in the top-left corner to see the difference.
Note that JPEG pictures which are loaded from the project resources and drawn in a similar way display just fine, i.e. have no pixelation.
Can anybody give me a hint as to why this is happening?
To elaborate a little, the byte array is obtained from Flickr as follows; this is based on code from the Photostream app by Romain Guy:
InputStream in = new BufferedInputStream(url.openStream(), IO_BUFFER_SIZE);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
copy(in, out);
out.flush();
final byte[] data = dataStream.toByteArray();
PS: I also posted a variant of this question on the android.developer Google group.
Thanks a lot for your suggestion -- now I am really puzzled! I did as you suggested and found that the image resulting directly from the downloaded byte array is indeed pixelated. However, this is downloaded from exactly the same URL which, when accessed on my computer, is NOT pixelated. Here is the corresponding Flickr URL:
http://farm3.static.flickr.com/2678/4315351421_54e8cdb8e5.jpg
Even stranger, when I run the same app in the simulator rather than on my phone (a HTC Hero), there is no pixelation.
How on earth is this possible?
Below is the code I use for loading a bitmap from a URL -- it is based on the Photostream app by Romain Guy, and it incorporates Will's suggestion to write the raw byte array to file:
Bitmap loadPhotoBitmap(URL url) {
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
FileOutputStream fos = new FileOutputStream("/sdcard/photo-tmp.jpg");
BufferedOutputStream bfs = new BufferedOutputStream(fos);
in = new BufferedInputStream(url.openStream(),
IO_BUFFER_SIZE);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
copy(in, out);
out.flush();
final byte[] data = dataStream.toByteArray();
bfs.write(data, 0, data.length);
bfs.flush();
BitmapFactory.Options opt = new BitmapFactory.Options();
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, opt);
} catch (IOException e) {
android.util.Log.e(LOG_TAG, "Could not load photo: " + this, e);
} finally {
closeStream(in);
closeStream(out)
closeStream(bfs);
}
return bitmap;
}
private static void copy(InputStream in, OutputStream out) throws IOException {
byte[] b = new byte[IO_BUFFER_SIZE];
int read;
while ((read = in.read(b)) != -1) {
out.write(b, 0, read);
}
}
private static void closeStream(Closeable stream) {
if (stream != null) {
try {
stream.close();
} catch (IOException e) {
android.util.Log.e(LOG_TAG, "Could not close stream", e);
}
}
}
Am I going crazy here?
Best,
Michael.
Ok, so I finally get it: it appears that my mobile network does image compression to save bandwidth.
Hence a picture downloaded from my phone is of lower quality than the same picture downloaded from my computer.
That's a bummer for my app, but I don't suppose there is anything I can do about it. Sigh.
Thanks again for your input though!
Best,
Michael.
Write the raw bytes fetched from the URL to /sdcard/tmp.jpg, and view on your PC.
JPEG images are compressed in 8x8 (or 16x16) tiles. The 'pixelation' as you describe it is actually in these tiles, suggesting that the 'bad' image is a JPEG that is more aggressively compressed than the other.
So I'd anticipate that the actual issue is that the image being downloaded is a very low-quality version, e.g. one intended for thumbnailing/preview use-cases.
Some version of Android have a bug in Bitmap class and convert the Bitmap to RGB_565 upon some operations. This would manifest itself in artifacts similar to those on your picture. This would also explain the banding of the blue sky.
Also, have in mind that android attempts to "optimize" image by converting them to rgb_565 upon loading and even compiling in resource files. Take a look at:
http://android.nakatome.net/2010/04/bitmap-basics.html