gpt4 book ai didi

android - 为什么我在Android平台上无法显示图片

转载 作者:行者123 更新时间:2023-11-30 03:37:19 26 4
gpt4 key购买 nike

在网站 http://218.93.33.59:85/map/zjmap/ibikegif.asp?flag=2&id=9 上,我们可以得到一个名为 ibikegif.gif 的 gif 图片。但是我无法在我的安卓手机上显示它。我的代码是这样的:

/**
*@param url the imageURL.it references to
*"http://218.93.33.59:85/map/zjmap/ibikegif.asp?flag=2&id=9" here
*
**/
public Bitmap returnBitMap(String url) {
URL myFileUrl = null;
Bitmap bitmap = null;
try {
myFileUrl = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl
.openConnection();
conn.setDoInput(true);
conn.connect();
InputStream is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is); //Attention:bitmap is null
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}

你能帮帮我吗?

最佳答案

我已经测试了这段代码并且它有效。这基本上就是您所做的,但有一点不同,我还将请求方法明确设置为 GET。

public class MyActivity extends Activity {

private ImageView imageView;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView = (ImageView) findViewById(R.id.imageView);
}

@Override
protected void onResume() {
super.onResume();
new AsyncTask<Void, Void, Void>() {
Bitmap bitmap;

@Override
protected Void doInBackground(Void... params) {
try{
bitmap = returnBitMap("http://218.93.33.59:85/map/zjmap/ibikegif.asp?flag=2&id=9");
} catch (Exception e) {
e.printStackTrace();
}
return null;
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
imageView.setImageBitmap(bitmap);
imageView.invalidate();
}

}.execute();
}

public Bitmap returnBitMap(String url) throws IOException {
URL myFileUrl = null;
Bitmap bitmap = null;
InputStream is = null;
try {
myFileUrl = new URL(url);
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
conn.setDoInput(true);
conn.setRequestMethod("GET");
conn.connect();
is = conn.getInputStream();
bitmap = BitmapFactory.decodeStream(is);
is.close();
} catch (IOException e) {
e.printStackTrace();
}
if(is != null) {
is.close();
}
return bitmap;
}
}

关于android - 为什么我在Android平台上无法显示图片,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16455342/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com