我正在使用Json(解码)从网址获取信息和图片,并使用CustomListviewAdapter
显示它们,但我有一个难以理解的问题,我无法找到解决方案。我没有收到任何错误(我的模拟器和手机),但有些手机正在提供bitmap size exceeds 32 bits error
。我必须解决这个问题,因为我的应用程序是在谷歌游戏市场。
顺便说一句,我正在使用urlimageviewhelper库来显示来自url的图像并使用延迟加载。 希望您能够帮助我。提前谢谢。
以下是代码
@Override
protected void onPostExecute(String result) {
super.onPostExecute(result);
try {
JSONArray jsonResponse = new JSONArray(result);
asd = new String[3][jsonResponse.length()];
rowItems = new ArrayList<RowItem>();
for (int i = 0; i < jsonResponse.length(); i++) {
JSONObject js = jsonResponse.getJSONObject(i);
asd[0][i] = js.getString("Category_Name");
asd[2][i] = js.getString("Image");
asd[1][i] = js.getString("Term_ID");
RowItem item = new RowItem(asd[2][i], asd[0][i], asd[1][i]);
rowItems.add(item);
}
adapter = new CustomListViewAdapter(MainActivity.this,
R.layout.list_item, rowItems);
listView.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
RowItem类
public class RowItem<T> {
private String imageId;
private String title;
private String desc;
public RowItem(String imageId, String title, String desc) {
this.imageId = imageId;
this.title = title;
this.desc = desc;
}
public String getImageId() {
return imageId;
}
public void setImageId(String imageId) {
this.imageId = imageId;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
@Override
public String toString() {
return title + "\n" + desc;
}
}
CustomListViewAdapter类
public class CustomListViewAdapter extends ArrayAdapter<RowItem> {
Context context;
Boolean isInternetPresent = false;
public CustomListViewAdapter(Context context, int resourceId,
List<RowItem> items) {
super(context, resourceId, items);
this.context = context;
}
/* private view holder class */
private class ViewHolder {
ImageView imageView;
TextView txtTitle;
TextView txtDesc;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
RowItem rowItem = getItem(position);
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item, null);
holder = new ViewHolder();
holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
holder.txtTitle = (TextView) convertView.findViewById(R.id.title);
holder.imageView = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtTitle.setText(rowItem.getTitle());
ImageView img = (ImageView) convertView.findViewById(R.id.icon);
ImageView gif = (ImageView) convertView.findViewById(R.id.icon);
Ion.with(gif).load("http://www.example.com/wp-content/uploads/2014/03/android-icon/loading.gif");
Drawable drawable=gif.getDrawable();
UrlImageViewHelper.setUrlDrawable(img, rowItem.getImageId(),drawable,
600000);
return convertView;
}
}
错误记录
java.lang.IllegalArgumentException: bitmap size exceeds 32bits
at android.graphics.Bitmap.nativeCreate(Native Method)
at android.graphics.Bitmap.createBitmap(Bitmap.java:809)
at android.graphics.Bitmap.createBitmap(Bitmap.java:769)
at android.view.View.buildDrawingCache(View.java:13649)
at android.view.View.getDisplayList(View.java:13379)
at android.view.View.getDisplayList(View.java:13445)
at android.view.View.draw(View.java:14223)
at android.view.ViewGroup.drawChild(ViewGroup.java:3118)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2955)
at android.view.View.getDisplayList(View.java:13398)
at android.view.View.getDisplayList(View.java:13445)
at android.view.View.draw(View.java:14223)
at android.view.ViewGroup.drawChild(ViewGroup.java:3118)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2955)
at android.view.View.getDisplayList(View.java:13398)
at android.view.View.getDisplayList(View.java:13445)
at android.view.View.draw(View.java:14223)
at android.view.ViewGroup.drawChild(ViewGroup.java:3118)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2955)
at android.view.View.getDisplayList(View.java:13398)
at android.view.View.getDisplayList(View.java:13445)
at android.view.View.draw(View.java:14223)
at android.view.ViewGroup.drawChild(ViewGroup.java:3118)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2955)
at android.view.View.getDisplayList(View.java:13398)
at android.view.View.getDisplayList(View.java:13445)
at android.view.View.draw(View.java:14223)
at android.view.ViewGroup.drawChild(ViewGroup.java:3118)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2955)
at android.view.View.getDisplayList(View.java:13398)
at android.view.View.getDisplayList(View.java:13445)
at android.view.View.draw(View.java:14223)
at android.view.ViewGroup.drawChild(ViewGroup.java:3118)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2955)
at android.view.View.getDisplayList(View.java:13398)
at android.view.View.getDisplayList(View.java:13445)
at android.view.View.draw(View.java:14223)
at android.view.ViewGroup.drawChild(ViewGroup.java:3118)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2955)
at android.view.View.getDisplayList(View.java:13398)
at android.view.View.getDisplayList(View.java:13445)
at android.view.View.draw(View.java:14223)
at android.view.ViewGroup.drawChild(ViewGroup.java:3118)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2955)
at android.view.View.getDisplayList(View.java:13398)
at android.view.View.getDisplayList(View.java:13445)
at android.view.View.draw(View.java:14223)
at android.view.ViewGroup.drawChild(ViewGroup.java:3118)
at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2955)
at android.view.View.draw(View.java:14509)
at android.widget.FrameLayout.draw(FrameLayout.java:472)
at com.android.internal.policy.impl.PhoneWindow$DecorView.draw(PhoneWindow.java:2452)
at android.view.View.getDisplayList(View.java:13403)
at android.view.View.getDisplayList(View.java:13445)
at android.view.HardwareRenderer$GlRenderer.buildDisplayList(HardwareRenderer.java:1570)
at android.view.HardwareRenderer$GlRenderer.draw(HardwareRenderer.java:1449)
at android.view.ViewRootImpl.draw(ViewRootImpl.java:2431)
at android.view.ViewRootImpl.performDraw(ViewRootImpl.java:2301)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1047)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5896)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
at android.view.Choreographer.doCallbacks(Choreographer.java:574)
at android.view.Choreographer.doFrame(Choreographer.java:544)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5105)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:608)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:-1)
try this :
String imageUrl ="http://www.example.com/wp-content/uploads/2014/03/android- icon/loading.gif";
public static int calculateSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
// Calculate ratios of height and width to requested height and
// width
final int heightRatio = Math.round((float) height
/ (float) reqHeight);
final int widthRatio = Math.round((float) width / (float) reqWidth);
// Choose the smallest ratio as inSampleSize value, this will
// guarantee
// a final image with both dimensions larger than or equal to the
// requested height and width.
inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
}
return inSampleSize;
}
InputStream in1 = convertURLtoInputStream(ImageUrl);
InputStream in2 = convertURLtoInputStream(ImageUrl);
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeStream(in1, null, options);
options.inSampleSize = calculateSampleSize(options, imgWidth , imgHeight);
options.inJustDecodeBounds = false;
bitmap = BitmapFactory.decodeStream(in2, null, options);
bitmapList.add(bitmap);