Igor
Igor

Reputation: 1464

Different size of same image on Web on Android

I have an image like test.png.

I need to purchase that image from the web and in another location I need to pick the same image from the drawable.

The problem is that I have the same png on the web and in the drawable. When I show the image I have in the drawable, it shows in the right size but when I get it from the web, it appears smaller. Just to make it clear, it's just an example of my problem... I'm not actually using the same image, they are different images but with the same size. To verify, I've uploaded the same image on the web and on drawable and found that the same image appears with different sizes. What do I need to do to make both appear like on drawable mode?

I've verified this:

When I get the image I've saved in SD card using:

File sdCard = Environment.getExternalStorageDirectory();
Fle directory = new File(sdCard.getAbsolutePath());
File file = new File(directory, "teste.png");
File InputStream streamIn;
Bitmap bitmap = Bitmapfactory.decodeStream(streamin);
ImageView image = new ImageView(c);
image.setImageBitmap(bitmap);

The image appears bigger than this way:

Drawable img = Drawable.createFromPath(new File(Environment.getExternalStorageDirectory(), "teste.png").getAbsolutePath());
ImageView image = new ImageView(c);
image.setImageDrawable(img);

But in the first mode it appears smaller than when I get the image directly from the resource drawable using getResouces.getDrawable...

Upvotes: 2

Views: 432

Answers (2)

Brais Gabin
Brais Gabin

Reputation: 5935

I wrote this code to reproduce the error:

LinearLayout ll = (LinearLayout) findViewById(R.id.root);

try {
    ImageView image = new ImageView(this);
    Bitmap bitmap = BitmapFactory.decodeStream(getAssets().open("tag-logo-android.png"));
    image.setImageBitmap(bitmap);
    ll.addView(image);

    image = new ImageView(this);
    Drawable drawable = Drawable.createFromStream(new URL("http://cdn.sstatic.net/stackoverflow/img/tag-logo-android.png").openStream(), null);
    image.setImageDrawable(drawable);
    ll.addView(image);
} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

The problem isn't the source of the image. The problem is that Drawable ignores screen density. This code works:

LinearLayout ll = (LinearLayout) findViewById(R.id.root);

try {
    ImageView image = new ImageView(this);
    Bitmap bitmap = BitmapFactory.decodeStream(getAssets().open("tag-logo-android.png"));
    image.setImageBitmap(bitmap);
    ll.addView(image);

    image = new ImageView(this);
    bitmap = BitmapFactory.decodeStream(new URL("http://cdn.sstatic.net/stackoverflow/img/tag-logo-android.png").openStream());
    image.setImageBitmap(bitmap);
    ll.addView(image);
} catch (MalformedURLException e) {
    e.printStackTrace();
} catch (IOException e) {
    e.printStackTrace();
}

In your case you can use:

bitmap = BitmapFactory.decodeFile(new File(Environment.getExternalStorageDirectory(), "test.png"))

Upvotes: 2

cassioso
cassioso

Reputation: 976

Maybe you need to use a Bitmap object instead Drawable to solve your problem.

Upvotes: 0

Related Questions