數據我有基本的下載類 在哪裏下載和解碼位爲好。 但如果我嘗試解碼了這個類的,讓我的位圖= NULL。失去愨類
public void add_item(String _txt, String _url) {
try {
Downloader dw = new Downloader(_url, context);
InputStream s = dw.execute().get();
Bitmap b = BitmapFactory.decodeStream(s); // <<<<<< bitmap is null
bmp.add(b);
} catch (ExecutionException e) { Toast.makeText(this.context, "Ошибка загрузки картинки", Toast.LENGTH_SHORT).show(); }
catch (InterruptedException e) { Toast.makeText(this.context, "Ошибка загрузки картинки", Toast.LENGTH_SHORT).show(); }
}
downloader.java
package com.example.john.weather;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.Toast;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
public class Downloader extends AsyncTask<Void, Void, InputStream> {
String url;
Context mContext;
public Downloader(String _url, Context _context){
this.url = _url;
this.mContext = _context;
}
@Override
protected InputStream doInBackground(Void... params) {
InputStream s;
try{
s = download(this.url);
//Bitmap b = BitmapFactory.decodeStream(s); // <<<<<< Bitmap is good
return s;
}
catch (MalformedURLException e) {
Toast.makeText(mContext, "Ошибка загрузки "+url, Toast.LENGTH_SHORT).show();
return null; }
catch (IOException e){
Toast.makeText(mContext, "Ошибка загрузки "+url, Toast.LENGTH_SHORT).show();
return null;
}
}
private InputStream download(String url) throws IOException {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.connect();
InputStream input = connection.getInputStream();
return input;
}
}
我有基本的下載類 在哪裏下載和解碼位爲好。 但如果我嘗試解碼出這個類,我得到我的位圖= null。
如何解碼出這個類? – eldjon 2014-08-30 15:50:02
位圖b = BitmapFactory.decodeStream(s); – user3017844 2014-08-30 15:55:42