2012-08-28 75 views
2

我有一个像test.png的图像。在Android上的Web上的相同图像的不同大小

我需要从网上购买该图像,并在另一个位置,我需要从drawable中选择相同的图像。

问题是我在网上和drawable中有相同的png。当我在可绘图中显示图像时,它显示的尺寸合适,但是当我从网页中获取图像时,它显得更小。 只是要说清楚,这只是我的问题的一个例子......我实际上并没有使用相同的图像,它们是不同的图像,但具有相同的尺寸。为了验证,我已经在网上上传了相同的图像并且绘制了,并且发现相同的图像以不同的尺寸出现。 我需要做什么才能使两者看起来像绘制模式?

我已经验证了这一点:

当我感到我已经保存在SD卡中的图像使用:

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); 

的图像会比这样大:

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

但在第一种模式下,它比使用getResouces.getDrawable直接从资源drawable获得图像时要小...

+0

您需要提供更多的情况下获得帮助与这个问题。你有什么尝试?你怎么实际显示每个版本(代码有帮助!)?当你上传图片时,你在用什么? (这可能会影响图像本身。) –

+0

即使您使用的图像相同,在转换为绘图时也会有不同的大小,因为它们是通过Context.getResources()以及其他参数(如scaleType等)创建的。Esp当在web视图上显示时,您可能会受到视点调整等的影响。也就是说,只需保存图像并以相同的方式恢复图像即可,因为它们将显示基本上相同的文件。 – Edison

+0

我完全不明白这个问题。当你从资源显示图像时,它看起来不错,但是当下载看起来更小时? –

回答

2

我写了这个代码重现错误:

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(); 
} 

的问题是不是图像的来源。问题是Drawable忽略屏幕密度。此代码的工作:

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(); 
} 

你的情况,你可以使用:

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

也许你需要使用Bitmap对象来代替Drawable来解决你的问题。

相关问题