为什么我能';t在Android平台上显示图像
在网站上为什么我能';t在Android平台上显示图像,android,gif,Android,Gif,在网站上http://218.93.33.59:85/map/zjmap/ibikegif.asp?flag=2&id=9,我们可以得到一个名为ibikegf.gif的gif图像。但是我不能在我的android手机上显示它。我的代码如下: /** *@param url the imageURL.it references to *"http://218.93.33.59:85/map/zjmap/ibikegif.asp?flag=2&id=9" here *
http://218.93.33.59:85/map/zjmap/ibikegif.asp?flag=2&id=9
,我们可以得到一个名为ibikegf.gif的gif图像。但是我不能在我的android手机上显示它。我的代码如下:
/**
*@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;
}
你能帮我吗?我已经测试了这段代码,它可以工作了。这基本上就是您所做的一个区别,我还显式地设置了请求方法来获取
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;
}
}
公共类MyActivity扩展活动{
私人影像视图;
/**
*在首次创建活动时调用。
*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView=(imageView)findViewById(R.id.imageView);
}
@凌驾
受保护的void onResume(){
super.onResume();
新建异步任务(){
位图;
@凌驾
受保护的Void doInBackground(Void…参数){
试一试{
位图=返回位图(“http://218.93.33.59:85/map/zjmap/ibikegif.asp?flag=2&id=9");
}捕获(例外e){
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(void避免){
super.onPostExecute(避免);
设置图像位图(位图);
imageView.invalidate();
}
}.execute();
}
公共位图返回位图(字符串url)引发IOException{
URL myFileUrl=null;
位图=空;
InputStream=null;
试一试{
myFileUrl=新URL(URL);
}捕获(格式错误){
e、 printStackTrace();
}
试一试{
HttpURLConnection conn=(HttpURLConnection)myFileUrl.openConnection();
conn.setDoInput(真);
conn.setRequestMethod(“GET”);
连接();
is=conn.getInputStream();
位图=BitmapFactory.decodeStream(is);
is.close();
}捕获(IOE异常){
e、 printStackTrace();
}
如果(is!=null){
is.close();
}
返回位图;
}
}
我已经测试了这段代码,它可以正常工作。这基本上就是您所做的一个区别,我还显式地设置了请求方法来获取
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;
}
}
公共类MyActivity扩展活动{
私人影像视图;
/**
*在首次创建活动时调用。
*/
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
imageView=(imageView)findViewById(R.id.imageView);
}
@凌驾
受保护的void onResume(){
super.onResume();
新建异步任务(){
位图;
@凌驾
受保护的Void doInBackground(Void…参数){
试一试{
位图=返回位图(“http://218.93.33.59:85/map/zjmap/ibikegif.asp?flag=2&id=9");
}捕获(例外e){
e、 printStackTrace();
}
返回null;
}
@凌驾
受保护的void onPostExecute(void避免){
super.onPostExecute(避免);
设置图像位图(位图);
imageView.invalidate();
}
}.execute();
}
公共位图返回位图(字符串url)引发IOException{
URL myFileUrl=null;
位图=空;
InputStream=null;
试一试{
myFileUrl=新URL(URL);
}捕获(格式错误){
e、 printStackTrace();
}
试一试{
HttpURLConnection conn=(HttpURLConnection)myFileUrl.openConnection();
conn.setDoInput(真);
conn.setRequestMethod(“GET”);
连接();
is=conn.getInputStream();
位图=BitmapFactory.decodeStream(is);
is.close();
}捕获(IOE异常){
e、 printStackTrace();
}
如果(is!=null){
is.close();
}
返回位图;
}
}
是否已将位图设置为imageview?imageview.setImageBitmap(位图);位图为空,因此我无法显示ImageView使用的图像。当您将conn.getResponseCode()放在conn.connect
之后时,它会返回什么?谢谢@iBecar,响应代码为200。可能没问题……是否已将位图设置为imageview?imageview.setImageBitmap(位图);位图为空,因此我无法显示ImageView使用的图像。当您将conn.getResponseCode()放在conn.connect
之后时,它会返回什么?谢谢@iBecar,响应代码为200。可能没问题……谢谢@iBecar。我调试了你的邮政编码,它在我的手机上不起作用。我调试代码,bitmap=BitmapFactory.decodeStream(is)
bitmap始终为空。我使用了张贴http://droid-blog.net/2011/10/15/tutorial-how-to-play-animated-gifs-in-android-%E2%80%93-第二部分/
,很有效。谢谢@iBecar。我调试了你的邮政编码,它在我的手机上不起作用。我调试代码,bitmap=BitmapFactory.decodeStream(is)
bitmap始终为空。我使用了张贴http://droid-blog.net/2011/10/15/tutorial-how-to-play-animated-gifs-in-android-%E2%80%93-第2部分/
,它可以工作。