Android 添加 ImageLoader 时 getInstance(this) 中的 Volley 错误
Android Volley error in getInstance(this) when adding ImageLoader
我正在跟进 image cashing tutorial using Volley Android 开发人员,我在请求图像请求和缓存它时遇到问题,我想是因为我创建的 singelton(从教程中复制) .
我的 Eclipse 在 getInstance(this)
中给出了错误,因为这是上下文,我正在请求一个我猜想的图像。
ImageRequest request = new ImageRequest(
url,
new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap bitmap) {
mNetworkImageView = (NetworkImageView) findViewById(R.id.ImageView);
mImageLoader = MySingleton.getInstance(this).getImageLoader();
mNetworkImageView.setImageUrl(IMAGE_URL, mImageLoader);
// mImageLoader = MySingleton.getInstance(this).getImageLoader();
// mImageLoader.get(IMAGE_URL, ImageLoader.getImageListener(mImageView,
// R.drawable.ic_launcher, R.drawable.ic_launcher));
}
},
0,
0,
null,
new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
// mImageView.setImageResource(R.drawable.ic_launcher);
}
});
MySingleton.getInstance(this).addToRequestQueue(request);
这是单例:
package com.example.p;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
public class MySingleton {
private static MySingleton mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mCtx;
private MySingleton(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
mImageLoader = new ImageLoader(
mRequestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(20);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
public static synchronized MySingleton getInstance(Context context) {
if (mInstance == null) {
mInstance = new MySingleton(context);
}
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
// getApplicationContext() is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {
getRequestQueue().add(req);
}
public ImageLoader getImageLoader() {
return mImageLoader;
}
}
我能够获取图像并以这种方式显示它,但我需要缓存它,所以我猜它是将它添加到请求中,对吗? .. 有帮助吗?
mNetworkImageView = (NetworkImageView) findViewById(R.id.ImageView);
mImageLoader = MySingleton.getInstance(this).getImageLoader();
mNetworkImageView.setImageUrl(IMAGE_URL, mImageLoader);
这是我的工作示例代码。希望对您有所帮助:
MainActivity.java:
import ...
public class MainActivity extends Activity {
final Context mContext = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NetworkImageView mNetworkImageView = (NetworkImageView) findViewById(R.id.networkImageView);
String mUrl = "http://192.168.0.100/api/getimage";
mNetworkImageView.setImageUrl(mUrl, VolleySingleton.getInstance(mContext).getImageLoader());
}
...
}
VolleySingleton.java:
public class VolleySingleton {
private static VolleySingleton mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mContext;
private VolleySingleton(Context context) {
mContext = context;
mRequestQueue = getRequestQueue();
mImageLoader = new ImageLoader(mRequestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<>(20);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
public static synchronized VolleySingleton getInstance(Context context) {
if (mInstance == null) {
mInstance = new VolleySingleton(context);
}
return mInstance;
}
private RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
// getApplicationContext() is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
mRequestQueue = Volley.newRequestQueue(mContext.getApplicationContext(), 10 * 1024 * 1024); // this for caching
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {
getRequestQueue().add(req);
}
public ImageLoader getImageLoader() {
return mImageLoader;
}
}
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity">
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/networkImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.volleyapp" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
你在错误的上下文中..初始化一个 class 成员
private final Context ctx = this;
然后在 onResponse 中使用 ctx
mImageLoader = MySingleton.getInstance(ctx).getImageLoader();
getInstance 方法的一个优化建议是使用双重检查锁定,因为它只在实例实际为空的情况下同步:
private volatile static VolleySingleton self;
public static VolleySingleton getInstance(Context context) {
if (self == null) {
//Using double checked locking
synchronized (VolleySingleton.class) {
if (self == null) {
//Using app context prevents leaking of activity context
self = new VolleySingleton(context.getApplicationContext());
}
}
}
return self;
}
可以找到有关单例和实例化它们的不同方法的更多信息here。
volatile 关键字确保所有线程立即看到该字段,并且不会在线程本地缓存。如果 getInstance 不需要 Context,link 中解释的其他方法比双重检查锁定更合适。
我正在跟进 image cashing tutorial using Volley Android 开发人员,我在请求图像请求和缓存它时遇到问题,我想是因为我创建的 singelton(从教程中复制) .
我的 Eclipse 在 getInstance(this)
中给出了错误,因为这是上下文,我正在请求一个我猜想的图像。
ImageRequest request = new ImageRequest(
url,
new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap bitmap) {
mNetworkImageView = (NetworkImageView) findViewById(R.id.ImageView);
mImageLoader = MySingleton.getInstance(this).getImageLoader();
mNetworkImageView.setImageUrl(IMAGE_URL, mImageLoader);
// mImageLoader = MySingleton.getInstance(this).getImageLoader();
// mImageLoader.get(IMAGE_URL, ImageLoader.getImageListener(mImageView,
// R.drawable.ic_launcher, R.drawable.ic_launcher));
}
},
0,
0,
null,
new Response.ErrorListener() {
public void onErrorResponse(VolleyError error) {
// mImageView.setImageResource(R.drawable.ic_launcher);
}
});
MySingleton.getInstance(this).addToRequestQueue(request);
这是单例:
package com.example.p;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.Volley;
import android.content.Context;
import android.graphics.Bitmap;
import android.support.v4.util.LruCache;
public class MySingleton {
private static MySingleton mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mCtx;
private MySingleton(Context context) {
mCtx = context;
mRequestQueue = getRequestQueue();
mImageLoader = new ImageLoader(
mRequestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<String, Bitmap>(20);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
public static synchronized MySingleton getInstance(Context context) {
if (mInstance == null) {
mInstance = new MySingleton(context);
}
return mInstance;
}
public RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
// getApplicationContext() is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
mRequestQueue = Volley.newRequestQueue(mCtx.getApplicationContext());
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {
getRequestQueue().add(req);
}
public ImageLoader getImageLoader() {
return mImageLoader;
}
}
我能够获取图像并以这种方式显示它,但我需要缓存它,所以我猜它是将它添加到请求中,对吗? .. 有帮助吗?
mNetworkImageView = (NetworkImageView) findViewById(R.id.ImageView);
mImageLoader = MySingleton.getInstance(this).getImageLoader();
mNetworkImageView.setImageUrl(IMAGE_URL, mImageLoader);
这是我的工作示例代码。希望对您有所帮助:
MainActivity.java:
import ...
public class MainActivity extends Activity {
final Context mContext = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NetworkImageView mNetworkImageView = (NetworkImageView) findViewById(R.id.networkImageView);
String mUrl = "http://192.168.0.100/api/getimage";
mNetworkImageView.setImageUrl(mUrl, VolleySingleton.getInstance(mContext).getImageLoader());
}
...
}
VolleySingleton.java:
public class VolleySingleton {
private static VolleySingleton mInstance;
private RequestQueue mRequestQueue;
private ImageLoader mImageLoader;
private static Context mContext;
private VolleySingleton(Context context) {
mContext = context;
mRequestQueue = getRequestQueue();
mImageLoader = new ImageLoader(mRequestQueue,
new ImageLoader.ImageCache() {
private final LruCache<String, Bitmap>
cache = new LruCache<>(20);
@Override
public Bitmap getBitmap(String url) {
return cache.get(url);
}
@Override
public void putBitmap(String url, Bitmap bitmap) {
cache.put(url, bitmap);
}
});
}
public static synchronized VolleySingleton getInstance(Context context) {
if (mInstance == null) {
mInstance = new VolleySingleton(context);
}
return mInstance;
}
private RequestQueue getRequestQueue() {
if (mRequestQueue == null) {
// getApplicationContext() is key, it keeps you from leaking the
// Activity or BroadcastReceiver if someone passes one in.
mRequestQueue = Volley.newRequestQueue(mContext.getApplicationContext(), 10 * 1024 * 1024); // this for caching
}
return mRequestQueue;
}
public <T> void addToRequestQueue(Request<T> req) {
getRequestQueue().add(req);
}
public ImageLoader getImageLoader() {
return mImageLoader;
}
}
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".MainActivity">
<com.android.volley.toolbox.NetworkImageView
android:id="@+id/networkImageView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.volleyapp" >
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
你在错误的上下文中..初始化一个 class 成员
private final Context ctx = this;
然后在 onResponse 中使用 ctx
mImageLoader = MySingleton.getInstance(ctx).getImageLoader();
getInstance 方法的一个优化建议是使用双重检查锁定,因为它只在实例实际为空的情况下同步:
private volatile static VolleySingleton self;
public static VolleySingleton getInstance(Context context) {
if (self == null) {
//Using double checked locking
synchronized (VolleySingleton.class) {
if (self == null) {
//Using app context prevents leaking of activity context
self = new VolleySingleton(context.getApplicationContext());
}
}
}
return self;
}
可以找到有关单例和实例化它们的不同方法的更多信息here。
volatile 关键字确保所有线程立即看到该字段,并且不会在线程本地缓存。如果 getInstance 不需要 Context,link 中解释的其他方法比双重检查锁定更合适。