如果操作系统版本是 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;
}
谁能帮忙??
这是日志猫:
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