0

I have a Bitmap (1538 x 2000) and set it as src of ImageView but when I run the app nothing shows up (but the activity background color), I read things about bitmap dimension limits, But if the problem is the limitation why I can view this bitmap by the gallery on the same device?

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">

<ImageView
    android:id="@+id/iv"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:scaleType="fitCenter"
    android:adjustViewBounds="true"
    android:src="@drawable/pic3"/>

 </FrameLayout>

Any help will be appreciated

Farshad
  • 3,074
  • 2
  • 30
  • 44

3 Answers3

2

You can try this.

put the image in the assets folder

src->main->assets

If the folder is not there, create it.

After putting the image there, use this code in java (setting image programmatically).

Drawable d = null;
    try {
        d = Drawable.createFromStream(getAssets().open("YOUR_IMAGE_NAME"), null);
    } catch (IOException e) {
        e.printStackTrace();
    }
    imageView.setImageDrawable(d);

Make sure you add the extension also when you use image name. eg: background.jpg or backgroung.png.

this should work.

The Bat
  • 1,085
  • 1
  • 13
  • 31
  • tnx @Shree , It worked,But I'm wondering why it was largening my bitmap and now the size is exact as the orginal version is – Farshad Mar 28 '16 at 13:48
  • @Farshad honestly, I am also not clear about this, but I faced this problem with big images. In devices with high ram and processor, it was working fine, but in other devices, it was either blank or the app was crashing. When I did programmatically, it worked. I tried to read about this, didn't understand much... but it has to do something with the allocation of cache memory – The Bat Mar 28 '16 at 13:50
  • The image was in the drawable folder, considering the logcat message android scales it 1.5 times that I don't know why! But when I load it as asset the result dimension is what I expected. – Farshad Mar 28 '16 at 14:05
0

There is a limit for image sizes in android. It's usually 2048 (max dimension), but you can get the device's specific value with these methods: Canvas.getMaximumBitmapWidth(), Canvas.getMaximumBitmapHeight()

Your image size seems ok, but maybe it's in a wrong drawable folder (and because of this the android read it with higher resolution). You can put the image to the drawable-nodpi (more info: http://developer.android.com/guide/practices/screens_support.html) folder to be sure it won't up/downscaled, but I personally think it would be better to use lower resolutions, maybe in multiple folders (or a vector assets, if the image is available in SVG)

Gabor Novak
  • 286
  • 1
  • 7
0

Well, I think the problem is my abuse of drawable folders without considering android behavior, I put the image in the drawable folder which android assumes it's proper for mdpi devices.

My logcat was pointing at this before :

OpenGLRenderer: Bitmap too large to be uploaded into a texture (2307x3000, max=2048x2048)

As you can see the original size was (1538x2000) and logcat reports it (2307x3000) which means android scales (1.5x) my image to fit my hdpi device and when I load it using assets as @Prakhar- the scarlett speedstr said, It worked how i expected without android interference.

Also I tried more and put the image in the drawable-hdpi and it worked fine without any scale.

Thanks for your helpful answers and comments.

Farshad
  • 3,074
  • 2
  • 30
  • 44