android 图片路径转drawable问题
想通过bitmap获取到http://180.168.93.163/assets/images/logo.png这张图片,然后把bitmap转换成drawable,现在遇...
想通过bitmap获取到http://180.168.93.163/assets/images/logo.png这张图片,然后把bitmap转换成drawable,
现在遇到的问题是用了好多种方法获取到的bitmap都是null值
Bitmap bitmap = BitmapFactory.decodeFile("http://180.168.93.163/assets/images/logo.png");
File file = new File("http://180.168.93.163/assets/images/logo.png");
bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
bitmap = BitmapFactory.decodeFile("http://180.168.93.163/assets/images/logo.png", options);
bitmap = BitmapFactory.decodeStream(inputStream);
用这些方法获取的bitmap都是null值
求教 展开
现在遇到的问题是用了好多种方法获取到的bitmap都是null值
Bitmap bitmap = BitmapFactory.decodeFile("http://180.168.93.163/assets/images/logo.png");
File file = new File("http://180.168.93.163/assets/images/logo.png");
bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
bitmap = BitmapFactory.decodeFile("http://180.168.93.163/assets/images/logo.png", options);
bitmap = BitmapFactory.decodeStream(inputStream);
用这些方法获取的bitmap都是null值
求教 展开
2个回答
展开全部
要用httpconnection去连接,成功后从返回值中getinputStream,这个inputStream才是可用于decode的。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package com.example.testbitmap;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.button1).setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
new Thread(new Runnable() {
@Override
public void run() {
byte[] data = getImage();
final Bitmap bitmap = BitmapFactory.decodeByteArray(
data, 0, data.length);
new Handler(getMainLooper()).post(new Runnable() {
@Override
public void run() {
((ImageView) findViewById(R.id.imageView1))
.setImageBitmap(bitmap);
}
});
}
}).start();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public static byte[] getImage() {
byte[] data = null;
try {
// 建立URL
URL url = new URL("
);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setReadTimeout(5000);
InputStream input = conn.getInputStream();// 到这可以直接BitmapFactory.decodeFile也行。 返回bitmap
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = input.read(buffer)) != -1) {
output.write(buffer, 0, len);
}
input.close();
data = output.toByteArray();
System.out.println("下载完毕!");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return data;
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询
广告 您可能关注的内容 |