我是java和android开发的新手。我试图找到这个问题的答案,但似乎这样没人问的是,是显而易见的.. 我用这个例子来显示图像:如何在android中显示bmp图像
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String str="http://logproj.500mb.net/image.php?id=8";
ImageView imView;
imView = (ImageView)findViewById(R.id.image);
try{
url = new URL(str);
}
catch(MalformedURLException e)
{
e.printStackTrace();
}
try{
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setDoInput(true);
conn.connect();
int length = conn.getContentLength();
int[] bitmapData =new int[length];
byte[] bitmapData2 =new byte[length];
InputStream is = conn.getInputStream();
bmp = BitmapFactory.decodeStream(is);
imView.setImageBitmap(bmp);
} catch (IOException e)
{
e.printStackTrace();
}
}
它的伟大工程为JPG图像,但我的形象是BMP和应用程序崩溃或“意外中止”。
我希望你能帮助解决这个问题。提前致谢。
您是否试图动态更改程序中的图像,或者您是否希望修复图像?如果图像已修复,则不需要对其进行编程,只需将其放入res下的drawable-hdpi文件夹中,然后将该图像设置为布局xml文件中的imgveiw即可。 – DrinkJavaCodeJava
所以我尝试了你所建议的一切,但没有任何工作,但最终我找到了解决方案:http://www.androidhive.info/2012/07/android-loading-image-from-url-http/。这真的对我有用。 当我的问题得到很多好的答案时,我感到非常惊喜。在我问其他网站之前(例如俄罗斯的Google Baraza),但是这些网站并没有专注于编程。非常感谢大家! – user1890184