我正在尝试使用异步请求从网址获取图片,以防止网址被挂起。这是我正在使用的代码
private void setImg(final ImageView im, String url){
AsyncHttpClient client = new AsyncHttpClient();
client.get(url, new AsyncHttpResponseHandler(){
public void onSuccess(String response){
try{
byte[] imageAsBytes = response.getBytes();
im.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));
im.refreshDrawableState();
} catch(Throwable e){
e.printStackTrace();
}
}
});
}
这始终在logcat中显示此警告
12-29 01:55:33.043: D/skia(14414): --- SkImageDecoder::Factory returned null
我找不到合适的理由。有什么帮助吗?
答案 0 :(得分:5)
现在已经将一个二进制响应处理程序添加到AsyncHttp中,您只需使用androids BitmapFactory.decodeByeArray函数:
AsyncHttpClient client = new AsyncHttpClient();
client.get(image_url, null, new AsyncHttpResponseHandler() {
@Override
public void onSuccess(byte[] fileData) {
Bitmap image = BitmapFactory.decodeByteArray(fileData, 0, fileData.length);
//Do whatever you want with the image variable
}
});
答案 1 :(得分:1)
我最近一直在使用以下库:UrlImageViewHelper。它使用 AsyncTask 来下载图像。你的代码是这样的:
private void setImg(final ImageView im, String url){
UrlImageViewHelper.setUrlDrawable(im, url) ;
}
现在很简单,我是对的吗?
答案 2 :(得分:1)
[UPDATE] 图像(和任何二进制数据)响应处理已添加到loopj的android-async-http库中。使用BinaryHttpResponseHandler
[老帖子] loopj的AsyncHttpClient还不支持处理byte []响应。有一个叉子,但它是一个烂摊子。所以答案就是其中之一:
A1)你没有。
A2)您可以通过此链接使用通过链接提供的分叉:https://github.com/loopj/android-async-http/issues/8
A3)你分叉AsyncHttpClient,添加byte []处理(不撕掉字符串响应处理,jesus!),并将其提交回项目。通过这样做,您还将获得开源业力信用。
答案 3 :(得分:1)
如果还有人在做这件事,我就是这样做的
public static void setImg(final ImageView im, final String url){
AsyncTask<Void, Void, Bitmap> t = new AsyncTask<Void, Void, Bitmap>(){
protected Bitmap doInBackground(Void... p) {
Bitmap bm = null;
try {
URL aURL = new URL(url);
URLConnection conn = aURL.openConnection();
conn.setUseCaches(true);
conn.connect();
InputStream is = conn.getInputStream();
BufferedInputStream bis = new BufferedInputStream(is);
bm = BitmapFactory.decodeStream(bis);
bis.close();
is.close();
} catch (IOException e) {
e.printStackTrace();
}
return bm;
}
protected void onPostExecute(Bitmap bm){
Bitmap output = Bitmap.createBitmap(bm.getWidth(), bm.getHeight(), Config.ARGB_8888);
Canvas canvas = new Canvas(output);
final int color = 0xff424242;
final Paint paint = new Paint();
final Rect rect = new Rect(0, 0, bm.getWidth(), bm.getHeight());
final RectF rectF = new RectF(rect);
final float roundPx = 5;
paint.setAntiAlias(true);
canvas.drawARGB(0, 0, 0, 0);
paint.setColor(color);
canvas.drawRoundRect(rectF, roundPx, roundPx, paint);
paint.setXfermode(new PorterDuffXfermode(Mode.SRC_IN));
canvas.drawBitmap(bm, rect, rect, paint);
im.setImageBitmap(output);
}
};
t.execute();
}
答案 4 :(得分:0)
public class MainActivity extends Activity {
ListView list;
public String IPadd = "http://api.gifts.com/v2/search/product.json?category=Nur&api_key=fd2ut5evb9jgzerjkeph54pz";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=(ListView) findViewById(R.id.listView1);
new AsyTask().execute(IPadd);
}
private class AsyTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... url) {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url[0]);
// HttpPost httpPost=new HttpPost(url[0]);
String text = null;
try {
HttpResponse response = httpClient.execute(httpGet);
text = EntityUtils.toString(response.getEntity());
} catch (Exception e) {
return e.getLocalizedMessage();
}
return text;
}
@Override
protected void onPostExecute(String result) {
try {
JSONObject jObject = new JSONObject(result);
JSONArray jarray = jObject.getJSONArray("products");
for (int i = 0; i < jarray.length(); i++) {
ProductInfo p=new ProductInfo();
JSONObject jObj = jarray.getJSONObject(i);
p.setTitle(jObj.getString("title"));
p.setId(jObj.getString("price"));
p.setImage(jObj.getString("largeProductImageUrl"));
ProductInfo.arrayList.add(p);
}
ArrAdapter adapter=new ArrAdapter(getApplicationContext(),ProductInfo.arrayList);
list.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
public class ArrAdapter extends BaseAdapter{
TextView id,title;
ImageView img;
Context context;
static ArrayList<ProductInfo> listitem=new ArrayList<ProductInfo>();
public ArrAdapter(Context applicationContext,ArrayList<ProductInfo> arrayList) {
this.context=applicationContext;
listitem=arrayList;
}
@Override
public int getCount() {
return listitem.size();
}
@Override
public Object getItem(int position) {
return listitem.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ProductInfo p=listitem.get(position);
if(convertView==null)
{
LayoutInflater inflater=(LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
convertView=inflater.inflate(R.layout.custom, null);
}
id=(TextView) convertView.findViewById(R.id.textView1);
id.setText(p.getId());
title=(TextView) convertView.findViewById(R.id.textView2);
title.setText(p.getTitle());
img=(ImageView) convertView.findViewById(R.id.imageView1);
ImageLoader imageloader=new ImageLoader(img);
imageloader.execute(p.getImage());
return convertView;
}
}
public class ProductInfo {
String ID,title,image;
static ArrayList<ProductInfo> arrayList=new ArrayList<ProductInfo>();
public void setId(String id)
{
this.ID=id;
}
public String getId()
{
return ID;
}
public void setTitle(String tit)
{
this.title=tit;
}
public String getTitle()
{
return title;
}
public void setImage(String img)
{
this.image=img;
}
public String getImage()
{
return image;
}
}
class ImageLoader extends AsyncTask<String, Void, Bitmap> {
ImageView bmImage;
public ImageLoader(ImageView bmImage) {
this.bmImage = bmImage;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap mIcon11 = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
mIcon11 = BitmapFactory.decodeStream(in);
} catch (Exception e) {
}
return mIcon11;
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
bmImage.setImageBitmap(result);
}
}