Best way to display an image from server in android - android

I am trying to display an image in android. Image needs to be taken from the server. What should be the best approach for making sure that the image stored at the server fits with all android device screens. In the image displaying XML I need to display a textview (below the image) also for giving a brief description about the image. Do I have to create the image with a specific height and width or is there any other approach?

You should store a large enough image on the server.
// Know the required width of the image
URL url = new URL(remotePath);
URLConnection urlConnection = url.openConnection();
urlConnection.connect();
inputStream = (InputStream) urlConnection.getContent();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(inputStream, options);
int height = options.outHeight;
int width = options.outWidth;
int sampleSize = requiredWidth / width; // Calculate how you want to sample the images so you can keep the memory small
options.inSampleSize = sampleSize;
options.inJustDecodeBounds = false;
Bitmap bitmap = BitmapFactory.decodeStream(inputStream, options);
imageView.setImageBitmap(bitmap);
Hope this helps.

If you want to display on that screen only the image and description below it, you could place the 2 component in a LinearLayout with the orientation vertical and use the layout_weight property of the LinearLayout, for example you could do something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="center" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
You could download the image from the server manually or you could use a library like UIL, or webImageLoader.

Related

Android photo in imageview, bad quality

I allow user to take photo, and I get this photo to set it in a imageview.
This is my code for my imageview :
<ImageView
android:id="#+id/photo1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:layout_marginBottom="2dp" />
And my code to put the photo in this imageview :
imgPhoto1.setImageBitmap(btmap);
My problem is, the photo is showing is blurred.....
How can I do to have a "correct" quality ?
I tried to use this :
getWindow().setFormat(PixelFormat.RGBA_8888);
but it changes nothing.
Thx,
EDIT : Please, I'm searching, if possible, a solution without to use a library, it's just for two imageview..
EDIT 2 : Ok, it seems impossible to do without library, because my image take all of space available on screen.
This function resolves the problem of bad quality of a image at ImageView:
public static Bitmap getBitmapFromResources(Resources resources, int resImage) {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = false;
options.inDither = false;
options.inSampleSize = 1;
options.inScaled = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
return BitmapFactory.decodeResource(resources, resImage, options);
}
And imageView.setImageBitmap(getBitmapFromResources(getResources(), R.drawable.image));
Instead of loading bitmap use universal image loader or picasso for this:
Android-Universal-Image-Loader
picasso
Why use Android Picasso library to download images?
You can also use AndroidQuery. It also allows you to load images async. I never had a quality issue with it. It is pretty easy to use and also allows you to cache images.
https://code.google.com/p/android-query/
change your imgview width and height style
<ImageView
android:id="#+id/photo1"
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent"-->
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="centerCrop"
android:adjustViewBounds="true"
android:layout_marginBottom="2dp" />
Hope It help

Big loss of Image quality when decode from InputStream

I'm trying to retrieve an image from a URL and show it on an ImageView. The task seems quite easy and i can show the images in the ImageView but the quality is really bad.
Here's what I did so far:
Options options = new BitmapFactory.Options();
options.inScaled = false;
options.inDither = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in,null,options);
The ImageView properties:
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
.../>
I also tried to directly use the decodeStream Without Options but the result is still the same.
The code is okay, you could also consider using dithering for better result. If the images you download are greatly smaller in pixels than the ImageView the result looks bad.
Solution is to have better quality images to download or change the ImageView to be smaller. You could adjust the ImageView width and height programmatically after you have decoded the Bitmap. If the Bitmap pixel size is lower than the ImageView, make the ImageView smaller.

Android list view image does not fit screen width

I am having this list view where the image view in it does not fit the screen width even if the size of the bitmap is larger than the screen size. Here is the result i get:
(Due the spam policy, I cannot post my screenshots over here. Please click on the links below to view the screenshots.)
http://www.sundancepost.com/ivue/screen/screen1.jpg
As indicated by red boxes in the screen above, the list does not fit the width of the screen.
The following is the result I wanted:
http://www.sundancepost.com/ivue/screen/screen2.jpg
Here is my code for the layout.
featured_listrow.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/banner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/ampersand_banner"/>
</RelativeLayout>
featured_list:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:smoothScrollbar="true"
>
</ListView>
</LinearLayout>
I believe I've set the layout_width and the layout_height correctly for both the layout files. I think this have something to do with the android:adjustViewBounds setting during runtime. Can somebody please help me?
I finally found out what is the problem. The scaling of the images happens in this ImageLoader.class found in http://www.androidhive.info/2012/02/android-custom-listview-with-image-and-text/
So, when I commented out the scaling part of the code, it all back to normal.
This is the code:
private Bitmap decodeFile(File f){
try {
//decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(new FileInputStream(f),null,o);
//Find the correct scale value. It should be the power of 2.
// final int REQUIRED_SIZE=100;
// int width_tmp=o.outWidth, height_tmp=o.outHeight;
int scale=1;
//while(true){
//if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
//break;
//width_tmp/=2;
//height_tmp/=2;
//scale*=2;
//}
//decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize=scale;
return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
} catch (FileNotFoundException e) {}
return null;
}
You can set your Image in your image view either using
1) android:scaleType="fitXY"
or
2) android:scaleType="centerCrop"
Use any one in your image view. it gives me perfect result as i want.
I think the only thing that could help here is resizing images using other programs. The problem is that image cannot be cropped programaticaly just horizontal or vertical sides it always do both when setting image size on xml or in code.

problems with the resolution of loaded images from url

I am loading images in an ImageView from url. (from RSS feed)
The problem is that the resolution of the loaded image is lower than the original image !!
I searched Stackoverflow and found that the solution is to set the "inScaled" option to false to prevent image re-scaling.
but, this solution didn't work for me.
Here is the code:
URL feedImage= new URL(img_link);
HttpURLConnection conn= (HttpURLConnection)feedImage.openConnection();
InputStream is = conn.getInputStream();
BitmapFactory.Options BFoptions = new BitmapFactory.Options();
BFoptions.inScaled = false;
Bitmap img = BitmapFactory.decodeStream(is,null, BFoptions);
int density = img.getDensity(); Log.e("img", "Image density is " + density);
int width =img.getWidth(); Log.e("img", "Image width is " + width);
int height = img.getHeight(); Log.e("img", "Image height is " + height);
my_image.setImageBitmap(img);
The variables density, width and height are lower than the original image's values.
and here is a part of my layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<ImageView
android:id="#+id/joke_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="temp description"/>
<ScrollView
android:id="#+id/SV"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:background="#drawable/background"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="50dp">
<!-- bla
bla
bla
-->
</ScrollView>
</RelativeLayout>
Thanks in advance
Finally it was solved :)
It appeared to be not related to the code or the ImageViews, .....
All the images in the RSS feeds are thumbnails ... which is the reason that they appear small in the app but large in the website.
So the solution was to replace the "_s.jpg" at the end of each image's url with "_n.jpg" :)
I hope this answer will help someone else :)

android - fitting image in imageview

I'm implementing a widget where i'm trying to display a large image inside an image view (8mpx) like this :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" android:id="#+id/widget"
android:background="#000000"
android:padding="15dp"
android:layout_margin="5dp"
android:layout_gravity="top|center_vertical|center_horizontal"
>
<LinearLayout android:background="#ffffff" android:padding="1dp" android:layout_width="wrap_content" android:layout_weight="1"
android:layout_height="wrap_content" >
<ImageView
android:adjustViewBounds="true"
android:id="#+id/image"
android:src="#drawable/sample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="top|center_horizontal"
android:scaleType="fitXY"
android:maxWidth="200dip"
android:maxHeight="300dip"
/>
</LinearLayout>
In the emulator everything seems ok, but when i deploy to device, i get the "problem loading widget" message.
the emulator is HVGA and my device has a 480x800 resolution.
Any ideea what am i doing wrong ?
Thank you!
==================================================
As advised by you guys i've made a screenshot of the logcat.
Here it is :
imageview.setScaleType(ScaleType.CENTER_INSIDE);
Try below code
<ImageView
android:adjustViewBounds="true"
android:id="#+id/image"
android:src="#drawable/sample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|center_horizontal"
android:scaleType="centerInside"
/>
use this
imageview.setImageResource(your_image);
imageview.setScaleType(ScaleType.MATRIX);
Old post, but you never know...
The logcat shows the problem:
"allocation too large for this process"
The image you are trying to render is too large to fit into memory. You need to scale the image down, but don't try to create a scaled version of the bitmap or you'll hit the same problem. The solution is the load the Bitmap into memory but ONLY for it's dimensions, then create a new Bitmap with a sample size that reduces the overall size of the image.
Actually you don't even need to load in the image to get it's original size to do this but it often makes sense so you can choose an appropriate sample size.
e.g.
Assuming your Bitmap is obtained from an InputStream:
InputStream in = ... // Your Bitmap Stream
// Decode JUST the dimensions
Options dimensionOptions = new Options();
dimensionOptions.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in, null, dimensionOptions);
// Get the dimensions of the raw
int rawWidth = dimensionOptions.outWidth;
// Choose a target width for the image (screen width would make sense)
int targetWidth = 480;
// Select the sample size which best matches our target size.
// This must be an int
float sampleSize = (float)rawWidth / (float)targetWidth;
// Assume lower, which will result in a larger image
int sample = (int) FloatMath.floor(sampleSize);
// Use this to decode the original image data
Options scaleOptions = new Options();
scaleOptions.inPreferredConfig = Bitmap.Config.ARGB_8888; // 4 bytes per pixel
scaleOptions.inSampleSize = sample;
// Now create a bitmap using the sample size
Bitmap scaled = BitmapFactory.decodeStream(in, null, scaleOptions);

Categories

Resources