0

I am new to Android Studio development, I am currently using UniversalImageLoader to display my image, but after I choose my image from the gallery, the app will crash. I am not sure what happen but here are the logs.

D/SelectPhotoDialog: onActivityResult: image uri: content://com.android.providers.media.documents/document/image%3A77 D/UploadFragment: getImagePath: setting the image to imageview D/AndroidRuntime: Shutting down VM E/AndroidRuntime: FATAL EXCEPTION: main Process: com.fyp.elvis.recapp, PID: 3151 java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=66770, result=-1, data=Intent { dat=content://com.android.providers.media.documents/document/image:77 flg=0x1 }} to activity {com.fyp.elvis.recapp/com.fyp.elvis.recapp.HomeActivity}: java.lang.IllegalStateException: ImageLoader must be init with configuration before using at android.app.ActivityThread.deliverResults(ActivityThread.java:4089) at android.app.ActivityThread.handleSendResult(ActivityThread.java:4132) at android.app.ActivityThread.-wrap20(ActivityThread.java) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1533) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:154) at android.app.ActivityThread.main(ActivityThread.java:6119) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) Caused by: java.lang.IllegalStateException: ImageLoader must be init with configuration before using at com.nostra13.universalimageloader.core.ImageLoader.checkConfiguration(ImageLoader.java:613) at com.nostra13.universalimageloader.core.ImageLoader.displayImage(ImageLoader.java:236) at com.nostra13.universalimageloader.core.ImageLoader.displayImage(ImageLoader.java:209) at com.nostra13.universalimageloader.core.ImageLoader.displayImage(ImageLoader.java:316) at com.fyp.elvis.recapp.util.UniversalImageLoader.setImage(UniversalImageLoader.java:46) at com.fyp.elvis.recapp.UploadFragment.getImagePath(UploadFragment.java:31) at com.fyp.elvis.recapp.SelectPhotoDialog.onActivityResult(SelectPhotoDialog.java:68) at android.support.v4.app.FragmentActivity.onActivityResult(FragmentActivity.java:151) at android.app.Activity.dispatchActivityResult(Activity.java:6932) at android.app.ActivityThread.deliverResults(ActivityThread.java:4085)

Here are some of the code that will be related to the UniversalImageLoader.

This is the code that capture the image path and set or display the image on the phone screen which I think the problem is here as display by the console log. It crashed after D/UploadFragment: getImagePath: setting the image to imageview.

public class UploadFragment extends Fragment implements SelectPhotoDialog.OnPhotoSelectedListener{

private static final String TAG = "UploadFragment";

@Override
public void getImagePath(Uri imagePath) {
    Log.d(TAG, "getImagePath: setting the image to imageview");
    UniversalImageLoader.setImage(imagePath.toString(), mUploadImage);
    //assign to global variable
    mSelectedBitmap = null;
    mSelectedUri = imagePath;
}

My UniversalImageLoader.java config is here.

public class UniversalImageLoader {

private static final int defaultImage = R.drawable.camera_icon;
private Context mContext;

public UniversalImageLoader(Context context) {
    mContext = context;
}

public ImageLoaderConfiguration getConfig(){
    DisplayImageOptions defaultOptions = new DisplayImageOptions.Builder()
            .showImageOnLoading(defaultImage)
            .showImageForEmptyUri(defaultImage)
            .showImageOnFail(defaultImage)
            .considerExifParams(true)
            .cacheOnDisk(true).cacheInMemory(true)
            .cacheOnDisk(true).resetViewBeforeLoading(true)
            .imageScaleType(ImageScaleType.EXACTLY)
            .displayer(new FadeInBitmapDisplayer(300)).build();

    ImageLoaderConfiguration configuration = new ImageLoaderConfiguration.Builder(mContext)
            .defaultDisplayImageOptions(defaultOptions)
            .memoryCache(new WeakMemoryCache())
            .diskCacheSize(100 * 1024 * 1024).build();

    return configuration;
}

public static void setImage(String imgURL, ImageView image){

    ImageLoader imageLoader = ImageLoader.getInstance();
    imageLoader.displayImage(imgURL, image);
}

}

Michael Dodd
  • 10,102
  • 12
  • 51
  • 64
Elvis
  • 31
  • 8
  • Too much code to read.. (as a developer) better start to learn debugging your code – B001ᛦ Feb 05 '18 at 13:17
  • I am sorry, I am really new to android development, and I am doing this for a school project, should I just display the code that is applicable? – Elvis Feb 05 '18 at 13:19
  • Please [edit] your code to reduce it to a [mcve] of your problem. Your current code includes much that is peripheral to your problem - a minimal sample normally looks similar to a good unit test: only performing one task, with input values specified for reproducibility. – Toby Speight Feb 05 '18 at 13:27
  • Edited. Thanks for the guide. – Elvis Feb 05 '18 at 13:47
  • https://stackoverflow.com/questions/17737858/android-imageloader-must-be-init-with-configuration-before-using-in-uil – vikas kumar Feb 05 '18 at 16:00
  • @vikaskumar thanks, i solved it myself by init it, sorry for the trouble and really thankful for the help – Elvis Feb 05 '18 at 16:11

0 Answers0