ImageView not displaying some images - android

I'm trying to display an image in Android's ImageView , but for an unknown reason some images are not displayed. The images that are working are relatively small and the ones not working are larger (1280x720) or so. I even tried to limit the image's size, by adding maxWidth or maxHeight in .xml file or even by code, but still no result.
Here's my .xml code for ImageView;
<ImageView
android:id="#+id/imageImageView"
android:layout_width="fill_parent"
android:maxHeight="100dp"
android:maxWidth="100dp"
android:layout_height="wrap_content"
android:contentDescription="#string/empty"
android:gravity="center"
android:src="#drawable/test3" />
And for my code:
ImageView imageView = (ImageView) view
.findViewById(R.id.imageImageView);
imageView.requestLayout();
imageView.getLayoutParams().height = 100;
imageView.getLayoutParams().width = 100;
Any ideas / suggestions ? Thank you.

Try resizing your image like :
private Drawable resize(Drawable image) {
Bitmap bitmap = ((BitmapDrawable) image).getBitmap();
Bitmap bitmapResized = Bitmap.createScaledBitmap(bitmap,
(int) (bitmap.getWidth() * 0.5), (int) (bitmap.getHeight() * 0.5), false);
return new BitmapDrawable(getResources(), bitmapResized);
}
And in your onCreateView() of your fragment:
ImageView imageView = (ImageView) view
.findViewById(R.id.imageImageView);
imageView.setImageResource(0);
Drawable draw = getResources().getDrawable(R.drawable.test3);
draw = resize(draw);
imageView.setImageDrawable(draw);

i had same problem sometime ago, my solution was checking the image naming convention example("-" to "_",no space within names, ...) you can check the java naming convention for more information.

Try this:
Add these two lines in xml code of your image view:
android:scaleType="fitXY"
android:adjustViewBounds="true"

<ImageView
android:id="#+id/imageImageView"
android:layout_width="100dp"
android:layout_height="100dp"
android:contentDescription="#string/empty"
android:src="#drawable/test3" />
Try this :)

For me i was getting this error
W/OpenGLRenderer: Bitmap too large to be uploaded into a texture (2250x4386, max=4096x4096)
my image size was actually 750x1462 but because it was sitting in the drawable-mdpi res bucket i think behind the scenes it was being scaled up, so i moved it to xxhdpi and then it loaded the image without a problem.

This might have come a little late. I think the filename could also be a problem. I just renamed mine from sebastian-staines-mfzdRsWsiRA-unsplash.jpg to sebastian_staines.jpg in my drawable folder in Android Studio and Boom, The Imageview Displayed it.
Hope this helps someone else

Related

ImageDrawable not set in desire imageview size

I'm trying to set drawable at ImageView where I'm generation drawable by image bitmap of intrinsicWidth and height 160 and imageview width is 36dp but still image not setting in 36dp imageview.
Image is looking more smaller than imageview.
Please help me.
<ImageView
android:id="#+id/img_doctor"
android:layout_width="36dp"
android:layout_height="36dp"
android:layout_centerVertical="true"
android:scaleType="centerCrop" />
Drawable drawable11 = new BitmapDrawable(getResources(),myBitmap);
Log.e("", ""+drawable11.getIntrinsicWidth()+"::"+drawable11.getIntrinsicHeight());
imgView.setImageDrawable(drawable11);
Here I'm getting 160::160 in logcat.
if you have bitmap already then use imgView.setImageBitmap(myBitmap) instead of converting bitmap to drawable.

Is it possible to display original image in android without fit to device views?

I have an Image of size 1000px*1200px and i want it to be displayed without being scaled!.
I have searched in stackover flow and google i am not getting good one.
I want full image without scaled,
I have tried like this
Step1:-
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image"
/>
Step2:-
I have tried with scaleType also
add a scaleType to your ImageView
<ImageView
android:scaleType="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/image" />
But this is also not working,
In sclate type i have tried with all possible properties (matrix,fitXY like this).
is there any possibility there for doing to dispaly original image in android?
Plz help me on this
You can try this approach,first get image from drawable and create a bitmap of it.Then get width and height of bitmap, resize your image view and then display image in it.It will create imageview with your original image size.
See below code -
BitmapDrawable bitmap = (BitmapDrawable) this.getResources().getDrawable(R.drawable.icon);
int bitmapHeight= bitmap .getBitmap().getHeight();
int bitmapWidth = bitmap .getBitmap().getWidth();
Now you have dimension of your original image.
Re-size image view -
ImageView imageView = (ImageView) findViewById(R.id.my_image_view);
imageView.getLayoutParams().height = bitmapHeight;
imageView.getLayoutParams().width = bitmapWidth;
and in last step set bitmap to your image view -
imageView.setImageBitmap(bitmap);
Hope it will help you.
EDITED
Use below code after resize image view -
replace - `imageView.setImageBitmap(bitmap);`
by
imageView.setImageResource(R.drawable.icon);
By your comments, I think what you need is a library like PhotoView.Hope it can help you
Although I would highly condemn such UX but whatever works for you.
1) Use the following link to make a two dimensional scroll.
Two dimensional scrolling... a suggestion that needs feedback
2) Add an imageview to the same and set your image dimensions for the imageview in px at runtime.
as far as i understand your question is that u want natural size of image without ImageView tag modifying it. This worked for me
<LinearLayout
android:id="#+id/background"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/image_here"
android:orientation="horizontal|vertical"/>
let me know in the comments.
Make your image layout width and height to wrap content. Make scaleType centre. Check your device width and height through device manager then give image height and width in %
After many trails i have find a solution for above question.
Here is the code:
MainActivity:-
public class MainActivity extends ActionBarActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = (ImageView) this.findViewById(R.id.imageview);
}
}
activity_main.xml:-
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:id="#+id/ScrollView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="none"
xmlns:android="http://schemas.android.com/apk/res/android">
<HorizontalScrollView android:id="#+id/HorizontalScrollView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView android:id="#+id/imageview"
android:src="#drawable/desert"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:adjustViewBounds="true">
</ImageView>
</HorizontalScrollView>
</ScrollView>
This is working perfectly for my requirement.
Hope this helps to others.

Android scaling imageview from setImageBitmap

I have this code:
<ImageView
android:id="#+id/listitem_logo"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
and
imageview_logo.setImageBitmap(bit); // comes from assets
imageview_logo.setAdjustViewBounds(true);
imageview_logo.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageview_logo.setBackgroundColor(0x00000000);
imageview_logo.setPadding(0, 0, 0, 0);
imageview_logo.setVisibility(v.VISIBLE);
When loading the image this way, no scaling seems to have been done. However, if I load an image through setImageDrawable() r.res. the image inside the ImageView is resized. However, I need to use setImageBitmap() since I load my images through assets folder.
Am I missing some setting here? That will make Android resize and scale the bitmap, so it uses the full width of the ImageView? I guess I can do it myself in code, but offhand I would think what I want to do would be supported just by setting some properties.
Can't trust system for everything specially android which behaves very differently sometimes.
You can resize the bitmap.
height = (Screenwidth*originalHeight)/originalWidth;
this will generate the appropriate height.
width is equal to screen width as you mentioned.
Bitmap pq=Bitmap.createScaledBitmap(pq,Screenwidth,height, true);
As I needed a bit of time to figure out the complete code to make this work, I am posting a full example here relying on the previous answer:
ImageView bikeImage = (ImageView) view.findViewById(R.id.bikeImage);
AssetLoader assetLoader = new AssetLoader(getActivity());
Bitmap bitmap = assetLoader.getBitmapFromAssets(
Constants.BIKES_FOLDER + "/" + bike.getName().toLowerCase()
.replace(' ', '_').replace('-', '_') + ".png");
int width = getActivity().getResources().getDisplayMetrics().widthPixels;
int height = (width*bitmap.getHeight())/bitmap.getWidth();
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
bikeImage.setImageBitmap(bitmap);
With bit as your Bitmap image, you can directly set yout imageView and then crop it using the properties of the imageView as:
imageview_logo.setImageBitmap(bit);
imageview_logo.setScaleType(ImageView.ScaleType.CENTER_CROP);

Prevent Image Scaling in Android App

I have an image that I am downloading from the internet in my android application. I do not want this image to be resized/scaled. Is there anyway I can stop the image from being scaled?
Here is my XML entry for the ImageView:
<ImageView
android:id="#+id/conditionImage"
android:scaleType="center"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/textMain"
android:contentDescription="#string/noaaLogoDescription" />
Here is my code for showing it in the image view:
BitmapFactory.Options bmpOptions = new Options();
bmpOptions.inScaled = false;
URL url = new URL("URLPATHTOIMAGE");
ImageView conditionImageView = (ImageView)findViewById(R.id.conditionImage);
InputStream content = (InputStream)url.getContent();
conditionImage = BitmapFactory.decodeStream(content, null, bmpOptions);
conditionImageView.setImageBitmap(conditionImage);
I have tried changing the scaleType in the ImageView XML element to use matrix, center, and centerInside. My last try was using the above code to set inScale to false. Does anyone have any other ideas?
The odd part is this is not happening in the emulator.
Have you put all images in simple "drawable" folder. I mean without extending hdpi,mdpi etc.
Try set
android:layout_width="wrap_content"
android:adjustViewBounds="true"
EDIT:
Not sure why, but changing
conditionImageView.setBackgroundDrawable(getResources().getDrawable(R.id.conditionImage));
to
conditionImageView.setImageDrawable(getResources().getDrawable(R.id.conditionImage));
solves my problem.
Adnan, using your example of giving the image view a fixed width and height and also removing the scaleType=centerImage fixed the issue.
This is a simple problem. The scaling happens when the ImageView is a lot larger than the rendered image itself. To avoid this ugly pixelated scailing, just "wrap content" the imageview and you should get your original image quality.
I resolved this problem by hardcoding height of imageview ( as I know it in advance) and then set the scaleType as cropCenter
see the below code for more clearity.
<ImageView
android:id="#+id/iv"
android:layout_width="match_parent"
android:layout_height="75dp"
android:scaleType="centerCrop" />

Remove ImageView auto-resizing

I want to put and ImageView with a large Y margin on my screen device, which would imply that part of the image would be out of screen, and then that the image would be cropped.
The problem is that Android is scaling the image all the time, so that it fits inside the screen, but I don't want that, I want the image to be cropped.
How can I force the ImageView to be cropped and not resized?
P.S. I tried all the possible ScaleType properties and none of them worked for me!
Code :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="200dp"
android:background="#drawable/my_image"
android:scaleType="centerCrop"
/>
</RelativeLayout>
Try replacing android:background with android:src.
First problem with your code is that you used:
android:background="#drawable/my_image"
instead of:
android:src="#drawable/my_image"
(with background none of the scaleType options work).
Now if this still doesn't help, you probably have to use scaleType="matrix" and then simply create a matrix that will do the required job. For example, let's assume that you want to:
keep the ratio of your image
scale the image so that the width parameter will equal X (for example: X can be the width of the screen)
make the top of the image visible (so crop the bottom of the image) - I'm assuming this is why centerCrop might not work for you
Here's the code:
ImageView imgView = (ImageView)findViewById(R.id.my_image_view);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
R.drawable.my_image);
Matrix matrix = new Matrix();
// Let's assume X = 400
float scale = ((float) 400) / bitmap.getWidth();
matrix.setScale(scale, scale);
imgView.setImageMatrix(matrix);
And remember to make the necessary changes in the xml file.

Categories

Resources