Reputation: 9070
I have an ImageView
like this
<ImageView
android:id="@+id/imageFromUrl"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:adjustViewBounds="true"
android:scaleType="fitCenter" />
and I have to display an image from an address I get from a webservice during runtime.
Note that the dimensions of the image is quite large (249x2902) but the size of the file is very small, about 32k.
I tried
URL url = new URL(urlToImage);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
imageFromUrl.setImageBitmap(myBitmap);
and this does not seem to render the image, it's just white.
I also tired
imageFromUrl.setImageURI(Uri.parse(urlToImage));
and that does not work either, the screen is white.
But if i use a WebView
and do
webView.loadUrl(urlToImage);
it loads fine.
What gives? Why does ImageView
not work?
I'm testing on a Galaxy Tab 2 where it is not working. If I try it on the emulator, everything is fine, ImageView works.
Upvotes: 0
Views: 702
Reputation: 13705
Couple of things here:
First, the size in Kbs doesn't mean it will not make the app crash because of the way android handle images, internally in the heap the file will take the memory of width * height * 4Bytes, a file of 1Kb size with huge dimensions could easily become several MBs, in order to download and handle big size files you should stick to the android best practices shown in the documentation, they provide a mechanism to check ImageBounds without loading it, and once calculated load the proper image size that fits whatever area you want.
Second, according to the code you are showing seems like you are downloading the image in the main UI thread, take on count that this action is prohibited in the latest android versions, throwing an Exception for using network calls in the main Thread. For a better response please post the logcat exception if any... but no matter what, you should never directly load such a big size image, read the link provided for further information
Hope this Helps.
Regards
Upvotes: 1