我是 java 和 android 开发的新手。我试图找到这个问题的答案,但似乎很明显,所以没有人问这个问题。我用这个例子来显示一个图像:
/** 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 并且应用程序崩溃或“意外停止”。
我希望你能帮助解决这个问题。提前致谢。