You first initialize Image Loader in a class that extend of Application not ?
And in the logcat what is the error exactly ?
Question
This is my first time using the Universal Image loader libary.
An error occurs when I use a button to trigger the display of a web image. The relevant codes looks like that:
public class MainActivity extends Activity {
ImageView iv;
Button btnButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
iv = (ImageView)findViewById(R.id.imageView1);
btnButton = (Button)findViewById(R.id.button1);
btnButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// dislpay a web image using a ImageView
String url = "http://icons.iconarchive.com/icons/xenatt/minimalism/128/App-Json-Toolbox-icon.png";
ImageLoader imageLoader = ImageLoader.getInstance();
ImageLoaderConfiguration imgconfig = ImageLoaderConfiguration.createDefault(MainActivity.this);
imageLoader.init(imgconfig);
imageLoader.displayImage(url, iv);
}
});
}
However, the program crushed. Based on the logcat, I find the error information in this sentence:
ImageLoaderConfiguration imgconfig = ImageLoaderConfiguration.createDefault(MainActivity.this);
How can I fix this error?
Solution 2
You first initialize Image Loader in a class that extend of Application not ?
And in the logcat what is the error exactly ?
OTHER TIPS
Create an Application like this :
import android.app.Application;
import com.nostra13.universalimageloader.cache.disc.naming.Md5FileNameGenerator;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.assist.QueueProcessingType;
public class CustomerApplication extends Application {
@Override
public void onCreate() {
// TODO Auto-generated method stub
super.onCreate();
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(
getApplicationContext())
.threadPriority(Thread.NORM_PRIORITY - 2)
.denyCacheImageMultipleSizesInMemory()
.discCacheFileNameGenerator(new Md5FileNameGenerator())
.tasksProcessingOrder(QueueProcessingType.LIFO)
.writeDebugLogs() // Remove for release app
.build();
// Initialize ImageLoader with configuration.
ImageLoader.getInstance().init(config);
}
@Override
public void onLowMemory() {
// TODO Auto-generated method stub
super.onLowMemory();
ImageLoader.getInstance().clearMemoryCache();
ImageLoader.getInstance().clearDiscCache();
}
}
don't forget to declare this in your manifest file.
<application
android:name="yourpackagename.application name"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name="Activity name"
android:label="@string/app_name"
android:screenOrientation="portrait"
android:theme="@style/Theme.MyAwesomeTheme" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
And Create instance on onCreate :
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.view_my_card);
mImageLoader = ImageLoader.getInstance();
mImageLoader.displayImage("Your Image URL", Your ImageView ,
Options); // "Options if necessary";
}
In my Case the options are:
public static DisplayImageOptions Options = new DisplayImageOptions.Builder()
.cacheInMemory(true).showImageOnFail(R.drawable.ic_launcher)
.showImageOnFail(R.drawable.ic_launcher)
.showImageOnLoading(R.drawable.ic_launcher).cacheOnDisc(true)
.bitmapConfig(Bitmap.Config.RGB_565).build();
Thats it, You're good to go.