Displaying images from the web? - android

How to display an image using a URL of a known jpg, png ..etc. File ?
I want an image , and I want it to be loaded from the internet from a particular website.
With that done,
I would like to make that image of an appropriate size...
Say I put the width as the same as screen_width (size)....What about the height ? I don't want to spoil the ratio of height/width of the original image...So is the height automatically set to account for the ratio ? or do I have to put in some value myself ...?
Plus, I want it to be zoomable.

To load images from website, you need to get the path from website server where the photo is stored. For image displaying put height as wrap_content mode so that the space will occupy as much as needed to display.

For this purpose you can use a Lazy Adapter. Here you can find a good example of it: https://stackoverflow.com/a/3068012/2436683. Maybe you can use it as a starting point.

Related

Load a very large Image in android

Assumes I have a picture, it very large images or other sets of content where you are only looking at small bits at a time, because you can start seeing your content without having to load it all into memory at once.
In iOs we can use CATiledLayer to repeatedly draw tiles to fill up view’s background
In Android I can see Google Map, It also load each part of map when you scroll but I don't understand what is solution of them.
I want know what is the solution same CATiledLayer in Android or other to load very large Image
you can actually scale down the bitmap according to the size of the image view.
Don't give wrap_content in width and height try to give a relative width and height.
use
ImageView.getheight()
ImageView.getWidth()
get the size and load according to it
see this link
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html#read-bitmap
You can use a library load images efficiently and manage caching them instead of downloading them again. I suggest Picasso or Glide. This tutorial compares between them and explains few features.
I hope it's useful.

Posting-a-photo usecase in android and server

I want to have the posting-a-photo usecase in my android app.
This is the scenario I have implemented:
1. User posts a photo.
2. On the client side I scale the photo* , compress it and send
it to the server.
3. I store the photo on the server.
4. Users, who can view the photo, fetch the photo from the server.
The problem with this approach is that it doesn't take into consideration
the different-screen-sizes problem.
Let's say user A posts a photo.
User B, which has a phone with a larger screen**, fetches the photo
from the server. The photo doesn't fit good in the environment
since it has fixed width and height determined by user A (scaling in step 2).
I cannot simply scale the photo and put it into user B's ImageView, because
I am risking to loose the aspect ratio of the photo.
One approach that comes to mind is to simply remove the step 2 from the scenario.
I send the photo to the server, and when a user tries to fetch it
I scale the image according to the user's screen size (which I receive as parameters in the fetch request).
The problem with this approach is that, on each request, I will have
to scale the photo on-the-fly and send it to the client.
Another problem is the efficiency of uploading a photo, because I don't scale the photo (before uploading it).
It takes much more time to upload a full 2MB (or even a 500KB) photo than to simply upload a scaled photo (~20KB).
What are my options?
Since I am relatively new to Android, chances are that I am missing something essential.
Cheers!
*I scale maintaining the aspect ratio.
** Sorry for the terminology, i don't know how and when to use high density/pixels correctly. :-)
I have own dedicated server with unlimited traffic and 4T HDD. So, basically what I do is:
User take a picture by phone
User upload image to the webserver
Using ImageMagick + mozjpeg 3 I resize image 3 times and save it with prefixes small_, medium_, large_ OR with prefixes pre size: 1024_, 640_...
Now, my app can find user's screen dimensions and load image by doing request for specified prefix.
My arguments for doing in this war are:
Free traffic
Big HDD
Fast user's internet speed
When upload in another thread, and show to user another activity, he doesn't feel the "long waiting time" for uploading large images.
You always can resize photo on device, from 10Mb to 2Mb, for example, and on server do other resizes.
Now about image ratio. There you cannot to do a lot. You must decide, what to do in case that user with large screen and own aspect ratio tries to load image that was uploaded by user with small screen and own aspect ratio.
The truth is that screen size doesn't matter. What is matter is a ratio. So, you can load image, put it in ImageView, and this will filled with black background if the image is "smaller". O r you can load large size of image, crop it and put it to the ImageView. Larger image is used only for better quality.
In my case, I always do crop images if I use it as preview. I think it's looks more beautiful. When user want to view full size image, I download full image and show it as is.
A good server that handles images should:
receive images at high-quality
save or cache the images in multiple sizes (small, medium, large)
serve images according to a size parameter (larger phones ask for the large image)
There are many solutions for this kind of server. Most of them resize the image on-the-fly and cache the result. The client tells the server what size it wants. One example is the image handling in Google Play. Go to the play webside and view the urls of the app-icons (example). They have a width parameter.
You can also think of a maximum dimension (like 2048px) and resize the image before upload only if needed.
The client that views the image needs to decide on what size it asks from the server or it can resize the image on the phone, instead of relying on the server. Do this if traffic is not an issue. Consider using Picasso or Glide to make things easier.
Regarding aspect ratio: The client that views the image should handle this. It should expect any aspect ratio and be prepared for it. Use android:scaleType on the ImageView and decide which scale type fits your scenario. Here are the scale types. If you don't want black lines and want to occupy as much as possible from the screen, use CENTER_CROP - but you might end up losing parts of the image.
In most apps the original aspect ratio in maintained and two black "stripes" are added if your screen doesn't match.
I think it's not possible to manage all screen ratios...
ImageView has android:scaleType attr that preserves aspect ration to be broken,
like fitCenter or centerInside
And also calculate size of fetched image and set view size by yourself.

How can I manage height and width of images coming from Google app engine in list view?

I am developing an android application using Google app engine.We have custom list view in which image has to be shown. I have used lazy loading for list view to load more data on demand.But problem is here, I am getting images from server, User can upload images of any size. Being used such images by user, I am not able to manage height and width for every image in list view List view is looking very ugly. How can I manage such images in specific height and width in image view?
Please share your best answer.
Thanks,
Prashant.
You can use the Google Images API for this. It has the capability to resize images have a look at
https://cloud.google.com/appengine/docs/java/images/
Using this you can
Resize
Rotate
Flip horizontally
Flip vertically
Crop
any image at runtime
Now when fetching the images use resize to scale down images to a specific dimension and use it in your listview
Simply there are 2 options.
Use android's ImageView.ScaleType
http://developer.android.com/reference/android/widget/ImageView.ScaleType.html
ImageView iv = someView;
iv.setScaleType(CENTER_CROP) // also CENTER_INSIDE, CENTER, whatever you want.
or in xml
<ImageView
...
android:scaleType="center" />
use blobstore magic url
https://cloud.google.com/appengine/docs/python/images/functions#Image_get_serving_url

Android Webview: Images that are too large for the screen

I'm looking for a way to have images appear in a WebView that initially fits the screen, instead of showing the actual size. Is there a way to do this?
How is it different if:
1) I open an html file with an img tag to the image
2) I open the image file directly
Thanks for any assistance.
I ended up using:
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setUseWideViewPort(true);
To make the contents of the page fit to the WebView.
If I understand the question, you have an image on a web page that has dimensions X x Y and when you load the webpage into a WebView you want it to take up the dimensions of the android device (X1 x Y1). To do this pass a querystring to the url which renders the height and the width of the image at 100%. Also have the layout_width and layout_height of the webview set to the appropriate values.
If you load the image directly into the WebView, you can set the height and width of the image
You can use Webview's LayoutAlgorithm. But this will resize only those images with Width greater than the width of the device. This won't help you with images smaller than the screen.
Link: Can Android's WebView automatically resize huge images?
Another Solution is using a Javascript or an External Library like HTML Cleaner to remove the
<img> size attributes and then change their size via code and getWidth().

How to find the co-ordinates of the transparent area of an .png image

In my application I use an image . I made some portion of the image transparent. Now How can I find the co-ordinates of the transparent portion of the image so that I am able to find the actual area of the non-transparent area.
I don't know the android SDK, but I doubt it has image manipulaiton built in. so using what ever language you are in, you need to open the image as data. Hopefully you can use an image library that will perform things like getting the size of the image. This will let you access the raw pixel data, which you can then check though till you find a pixel whos transparency is not equal to zero.
finding it in the rows is fairly easy, but the columns will take a bit more work. Remember that pixel data will be accessed as an array of length width * height in pixels.

Categories

Resources