如果操作系统版本为4.3,“BitmapFactory.decodeStream(inputStream)”在AsyncTask中返回null有一点问题,但如果操作系统版本是2.3.3则运行良好,这里有一个示例:
public class GetImagesTask extends AsyncTask<Void, Void, InputStream> {
private URL url;
private BitmapFactory.Options opts;
private ImageView imageView;
public GetImagesTask(URL url, BitmapFactory.Options opts,
ImageView imageView) {
this.url = url;
this.opts = opts;
this.imageView = imageView;
}
@Override
protected InputStream doInBackground(Void... arg0) {
// TODO Auto-generated method stub
InputStream inputStream = null;
try {
inputStream = url.openConnection().getInputStream();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
return inputStream;
}
@Override
protected void onPostExecute(InputStream inputStream) {
// TODO Auto-generated method stub
super.onPostExecute(inputStream);
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(inputStream);
System.out.println();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
} else {
imageView.setImageResource(R.drawable.loader);
}
}
}
这里我称之为asynctask,:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Model.LoadModel(items);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(rowResourceId, parent, false);
ImageView imageView = (ImageView) rowView.findViewById(R.id.imageView);
TextView textView = (TextView) rowView.findViewById(R.id.textView);
int id = items.get(position).getId();
String imageFile = Model.GetbyId(id).iconFile;
textView.setText(Model.GetbyId(id).name);
// Loader image - will be shown before loading image
int loader = R.drawable.loader;
// Image url
String image_url = "IP and PORT" + imageFile;
URL url = null;
Bitmap bmp = null;
try {
url = new URL(image_url);
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inDither = false; // Disable Dithering mode
opts.inPurgeable = true; // Tell to gc that whether it needs free
// memory, the Bitmap can be cleared
opts.inInputShareable = true; // Which kind of reference will be
// used to recover the Bitmap data
// after being clear, when it will
// be used in the future
opts.inTempStorage = new byte[32 * 1024];
opts.inSampleSize = 8;
getImagesTask = new GetImagesTask(url, opts, imageView);
getImagesTask.execute((Void) null);
return rowView;
}
任何人都可以帮忙吗?
这是logcat:
09-05 08:15:59.975: W/System.err(1060): at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
09-05 08:15:59.975: W/System.err(1060): at java.io.BufferedInputStream.read(BufferedInputStream.java:304)
09-05 08:15:59.975: W/System.err(1060): at libcore.net.http.FixedLengthInputStream.read(FixedLengthInputStream.java:45)
09-05 08:15:59.975: W/System.err(1060): at java.io.BufferedInputStream.fillbuf(BufferedInputStream.java:168)
09-05 08:15:59.975: W/System.err(1060): at java.io.BufferedInputStream.read(BufferedInputStream.java:309)
09-05 08:15:59.975: W/System.err(1060): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
09-05 08:15:59.975: W/System.err(1060): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:530)
09-05 08:15:59.975: W/System.err(1060): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:603)
09-05 08:15:59.975: W/System.err(1060): at com.domainintegrators.fitnessyard.tasks.GetImagesTask.onPostExecute(GetImagesTask.java:49)
09-05 08:15:59.975: W/System.err(1060): at com.domainintegrators.fitnessyard.tasks.GetImagesTask.onPostExecute(GetImagesTask.java:1)
09-05 08:15:59.975: W/System.err(1060): at android.os.AsyncTask.finish(AsyncTask.java:631)
09-05 08:15:59.975: W/System.err(1060): at android.os.AsyncTask.access$600(AsyncTask.java:177)
09-05 08:15:59.975: W/System.err(1060): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
09-05 08:15:59.975: W/System.err(1060): at android.os.Handler.dispatchMessage(Handler.java:99)
09-05 08:15:59.975: W/System.err(1060): at android.os.Looper.loop(Looper.java:137)
09-05 08:15:59.975: W/System.err(1060): at android.app.ActivityThread.main(ActivityThread.java:5103)
09-05 08:15:59.975: W/System.err(1060): at java.lang.reflect.Method.invokeNative(Native Method)
09-05 08:15:59.975: W/System.err(1060): at java.lang.reflect.Method.invoke(Method.java:525)
09-05 08:15:59.975: W/System.err(1060): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
09-05 08:15:59.975: W/System.err(1060): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
09-05 08:15:59.975: W/System.err(1060): at dalvik.system.NativeStart.main(Native Method)
09-05 08:15:59.975: D/skia(1060): ---- read threw an exception
09-05 08:15:59.975: D/skia(1060): --- SkImageDecoder::Factory returned null
答案 0 :(得分:0)
您无法在UI线程中使用互联网连接。重新定位
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(inputStream);
System.out.println();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
到doInBackground程序部分和
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
}
留在onPostExecute。 Actualy你不能在&#34; doInBackground&#34;只得到连接。您在后台和UI线程中的所有工作仅用于操纵UI元素。