您好我正在使用Volley在我的应用中维护缓存。但我经常得到OOM异常。请帮帮我
SimpleViewFragPhone.java
public class SimpleViewFragPhone extends Fragment {
public static final String TAG = SimpleViewFragPhone.class.getSimpleName();
private Gallery mGallery;
private List<MyCataLogueData> simpleViewList = new ArrayList<MyCataLogueData>();
MainActivityPhone mainActivity;
private int height;
private int width;
private int mItemWidth;
private SimpleViewAdapter mAdapter;
private Button back;
//private TextView title;
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mainActivity = (MainActivityPhone) activity;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Display display = getActivity().getWindowManager().getDefaultDisplay();
height = display.getHeight();
width = display.getWidth();
mItemWidth = (int) (width / 1.18f);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = null;
v = inflater.inflate(R.layout.simple_view_phone, container, false);
mGallery = (Gallery) v.findViewById(R.id.simpleViewGalleruy);
mAdapter = new SimpleViewAdapter(simpleViewList, height, mItemWidth, inflater, getActivity().getApplicationContext());
mGallery.setAdapter(mAdapter);
DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
parseJsonForCataLog();
mGallery.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
Log.e(TAG, "position: ====="+position);
}
});
return v;
}
protected void parseJsonForCataLog() {
try {
String jsonObjectString = getArguments().getString("JSONObject");
JSONObject rootResponce = new JSONObject(jsonObjectString);
Log.e(TAG, "rootResponce: " + rootResponce);
JSONArray simpleArray = rootResponce.optJSONArray("SIMPLE");
if(simpleArray == null)
return;
for (int i = 0; i < simpleArray.length(); i++) {
JSONObject childMenuObject = simpleArray.getJSONObject(i);
String textSimple = childMenuObject.optString("text");
String titleSimple = childMenuObject.optString("title");
String imageUrlSimple = childMenuObject.optString("imageUrl");
MyCataLogueData mcld = new MyCataLogueData("SimpleContent" ,textSimple, titleSimple, imageUrlSimple);
simpleViewList.add(mcld);
}
mAdapter.notifyDataSetChanged();
} catch (Exception e) {
e.printStackTrace();
}
}
}
和SimpleViewAdapter是
public class SimpleViewAdapter extends BaseAdapter {
protected static final String TAG = SimpleViewAdapter.class.getSimpleName();
private List<MyCataLogueData> simpleViewList;
private int height;
private int mItemWidth;
private LayoutInflater inflater;
private Context context;
public SimpleViewAdapter(List<MyCataLogueData> simpleViewList, int height,
int mItemWidth, LayoutInflater inflater, Context context) {
this.simpleViewList = simpleViewList;
this.height = height;
this.mItemWidth = mItemWidth;
this.inflater = inflater;
this.context = context;
}
static class ViewHolder {
public TextView tvText = null;
}
@Override
public int getCount() {
return simpleViewList.size();
}
@Override
public Object getItem(int position) {
return simpleViewList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
NetworkImageView networkImageView = null;
TextView tvTitle = null;
View childView = null;
ImageView loader;
final Button moreBtn;
final RelativeLayout contentRl;
Animation mRotateAnim = AnimationUtils.loadAnimation(context, R.anim.rotate_and_scale);
final RelativeLayout.LayoutParams paramsOfContent = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
final int conteHeight = (int) (height * 0.23f);
if (convertView == null) {
childView = inflater.inflate(R.layout.simple_view_content_item, null);
} else {
childView = convertView;
}
if (childView != null) {
childView.setLayoutParams(new Gallery.LayoutParams(
mItemWidth, LayoutParams.MATCH_PARENT));
networkImageView = (NetworkImageView) childView
.findViewById(R.id.networkImageView);
networkImageView.getLayoutParams().width = mItemWidth;
networkImageView.getLayoutParams().height = LayoutParams.MATCH_PARENT;
loader = (ImageView) childView.findViewById(R.id.indicator_volley);
moreBtn = (Button) childView.findViewById(R.id.more_btn);
contentRl = (RelativeLayout) childView.findViewById(R.id.content_rl);
paramsOfContent.height = conteHeight;
paramsOfContent.width = LayoutParams.MATCH_PARENT;
paramsOfContent.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
contentRl.setLayoutParams(paramsOfContent);
MyCataLogueData cld = simpleViewList.get(position);
if (networkImageView != null) {
loader.startAnimation(mRotateAnim);
networkImageView
.setImageUrl(cld.getSimpleImageUrl(), ImageCacheManager.getInstance().getImageLoader(), loader);
}
tvTitle = (TextView) childView.findViewById(R.id.title);
if (tvTitle != null) {
tvTitle.setText(cld.getSimpleTitle());
}
final ViewHolder viewHolder = new ViewHolder();
viewHolder.tvText = (TextView) childView.findViewById(R.id.text);
if (viewHolder.tvText != null) {
viewHolder.tvText.setText(cld.getSimpleText());
}
moreBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String tag = (String) v.getTag();
if (tag.equalsIgnoreCase("0")) {
Log.e(TAG, ""+tag);
moreBtn.setTag("1");
viewHolder.tvText.setMaxLines(Integer.MAX_VALUE);
paramsOfContent.height = LayoutParams.MATCH_PARENT;
paramsOfContent.width = LayoutParams.MATCH_PARENT;
contentRl.setLayoutParams(paramsOfContent);
contentRl.requestLayout();
contentRl.setAnimation(UCUtils.outToTopAnimation());
moreBtn.setText(context.getResources().getString(R.string.back));
} else if (tag.equalsIgnoreCase("1")){
Log.e(TAG, ""+tag);
moreBtn.setTag("0");
viewHolder.tvText.setMaxLines(2);
paramsOfContent.height = conteHeight;
paramsOfContent.width = LayoutParams.MATCH_PARENT;
paramsOfContent.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
contentRl.setLayoutParams(paramsOfContent);
contentRl.requestLayout();
moreBtn.setText(context.getResources().getString(R.string.more_info));
}
}
});
}
return childView;
}
}
MyAppication.java
public class MyAppication extends Application {
private static int DISK_IMAGECACHE_SIZE = 1024*1024*30;
private static CompressFormat DISK_IMAGECACHE_COMPRESS_FORMAT = CompressFormat.PNG;
private static int DISK_IMAGECACHE_QUALITY = 100; //PNG is lossless so quality is ignored but must be provided
@Override
public void onCreate() {
init();
}
/**
* Intialize the request manager and the image cache
*
* @author Gangadhar
*/
private void init() {
RequestManager.init(this);
createImageCache();
}
/**
* Create the image cache.
*/
private void createImageCache(){
ImageCacheManager.getInstance().init(this,
this.getPackageCodePath()
, DISK_IMAGECACHE_SIZE
, DISK_IMAGECACHE_COMPRESS_FORMAT
, DISK_IMAGECACHE_QUALITY);
}
}
RequestManager.Java
public class RequestManager {
/**
* the queue :-)
*/
private static RequestQueue mRequestQueue;
/**
* Nothing to see here.
*/
private RequestManager() {
// no instances
}
/**
* @param context
* application context
*/
public static void init(Context context) {
mRequestQueue = Volley.newRequestQueue(context);
}
/**
* @return
* instance of the queue
* @throws
* IllegalStatException if init has not yet been called
*/
public static RequestQueue getRequestQueue() {
if (mRequestQueue != null) {
return mRequestQueue;
} else {
throw new IllegalStateException("Not initialized");
}
}
}
ImageCacheManager.java
public class ImageCacheManager implements ImageCache{
private static ImageCacheManager mInstance;
/**
* Volley image loader
*/
private ImageLoader mImageLoader;
/**
* Image cache used for local image storage
*/
private DiskLruImageCache mDiskCache;
/**
* @return
* instance of the cache manager
*/
public static ImageCacheManager getInstance(){
if(mInstance == null)
mInstance = new ImageCacheManager();
return mInstance;
}
/**
* Initializer for the manager. Must be called prior to use.
*
* @param context
* application context
* @param uniqueName
* name for the cache location
* @param cacheSize
* max size for the cache
* @param compressFormat
* file type compression format.
* @param quality
*/
public void init(Context context, String uniqueName, int cacheSize, CompressFormat compressFormat, int quality){
mDiskCache = new DiskLruImageCache(context, uniqueName, cacheSize, compressFormat, quality);
mImageLoader = new ImageLoader(RequestManager.getRequestQueue(), this);
}
@Override
public Bitmap getBitmap(String url) {
try {
return mDiskCache.getBitmap(createKey(url));
} catch (NullPointerException e) {
throw new IllegalStateException("Disk Cache Not initialized");
}
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
try {
mDiskCache.put(createKey(url), bitmap);
} catch (NullPointerException e) {
throw new IllegalStateException("Disk Cache Not initialized");
}
}
/**
* Executes and image load
* @param url
* location of image
* @param listener
* Listener for completion
*/
public void getImage(String url, ImageListener listener){
mImageLoader.get(url, listener);
}
/**
* @return
* instance of the image loader
*/
public ImageLoader getImageLoader() {
return mImageLoader;
}
/**
* Creates a unique cache key based on a url value
* @param url
* url to be used in key creation
* @return
* cache key value
*/
private String createKey(String url){
return String.valueOf(url.hashCode());
}
}
和logcat是
11-25 19:43:28.607: E/AndroidRuntime(9158): FATAL EXCEPTION: main
11-25 19:43:28.607: E/AndroidRuntime(9158): java.lang.OutOfMemoryError
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:530)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:603)
11-25 19:43:28.607: E/AndroidRuntime(9158): at my.package.cms.cache.DiskLruImageCache.getBitmap(DiskLruImageCache.java:115)
11-25 19:43:28.607: E/AndroidRuntime(9158): at my.package.cms.cache.ImageCacheManager.getBitmap(ImageCacheManager.java:62)
11-25 19:43:28.607: E/AndroidRuntime(9158): at com.android.volley.toolbox.ImageLoader.get(ImageLoader.java:194)
11-25 19:43:28.607: E/AndroidRuntime(9158): at com.android.volley.toolbox.ImageLoader.get(ImageLoader.java:171)
11-25 19:43:28.607: E/AndroidRuntime(9158): at com.android.volley.toolbox.NetworkImageView.loadImageIfNecessary(NetworkImageView.java:151)
11-25 19:43:28.607: E/AndroidRuntime(9158): at com.android.volley.toolbox.NetworkImageView.onLayout(NetworkImageView.java:202)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.view.View.layout(View.java:14289)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.view.View.layout(View.java:14289)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.view.ViewGroup.layout(ViewGroup.java:4559)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.widget.Gallery.setUpChild(Gallery.java:923)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.widget.Gallery.makeAndAddView(Gallery.java:858)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.widget.Gallery.layout(Gallery.java:665)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.widget.Gallery.onLayout(Gallery.java:357)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.view.View.layout(View.java:14289)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.view.ViewGroup.layout(ViewGroup.java:4559)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.view.View.layout(View.java:14289)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.view.ViewGroup.layout(ViewGroup.java:4559)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.view.View.layout(View.java:14289)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.view.ViewGroup.layout(ViewGroup.java:4559)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.view.View.layout(View.java:14289)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.view.ViewGroup.layout(ViewGroup.java:4559)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.view.View.layout(View.java:14289)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.view.ViewGroup.layout(ViewGroup.java:4559)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.view.View.layout(View.java:14289)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.view.ViewGroup.layout(ViewGroup.java:4559)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.view.View.layout(View.java:14289)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.view.ViewGroup.layout(ViewGroup.java:4559)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.view.View.layout(View.java:14289)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.view.ViewGroup.layout(ViewGroup.java:4559)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1589)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.view.View.layout(View.java:14289)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.view.ViewGroup.layout(ViewGroup.java:4559)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.view.View.layout(View.java:14289)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.view.ViewGroup.layout(ViewGroup.java:4559)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.view.View.layout(View.java:14289)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.view.ViewGroup.layout(ViewGroup.java:4559)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.view.View.layout(View.java:14289)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.view.ViewGroup.layout(ViewGroup.java:4559)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1076)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.view.View.layout(View.java:14289)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.view.ViewGroup.layout(ViewGroup.java:4559)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.view.View.layout(View.java:14289)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.view.ViewGroup.layout(ViewGroup.java:4559)
11-25 19:43:28.607: E/AndroidRuntime(9158): at android.widget.LinearLayout.setCh