2015-01-21 59 views
0

我想从给定的URL获取位图,因为我使用的是UIL库。出于某种原因,我无法获取位图。这是我正在尝试的。从通用图像加载器库获取位图

初始化的变量:

private ImageLoader imageLoader; 
    private ImageLoaderConfiguration config; 

然后有配置初始化ImageLoader的:

config = new ImageLoaderConfiguration.Builder(this) 
    .threadPriority(Thread.NORM_PRIORITY - 2) 
    .denyCacheImageMultipleSizesInMemory() 
    .diskCacheFileNameGenerator(new Md5FileNameGenerator()) 
    .diskCacheSize(50 * 1024 * 1024) // 50 Mb 
    .tasksProcessingOrder(QueueProcessingType.LIFO) 
    .writeDebugLogs() // Remove for release app 
    .build(); 

    ImageLoader.getInstance().init(config); 

然后试图让提供的图片网址:

Bitmap bmp = imageLoader.loadImageSync(ImageUrl); 

存储将bmp数据转换为数组以供进一步使用。

我收到以下错误:

Attempt to invoke virtual method 'android.graphics.Bitmap com.nostra13.universalimageloader.core.ImageLoader.loadImageSync(java.lang.String)' on a null object reference 

我不知道这可能是错在这里?有人可以帮我解决这个问题吗?

+0

你在哪里初始化'imageLoader'变量? – Michael 2015-01-21 11:42:33

回答

1

imageLoader为空,因为你没有设置:

试试这个:

imageLoader = ImageLoader.getInstance(); 
imageLoader.init(config); 
0

公共类CustomVolleyRequestQueue {

private static CustomVolleyRequestQueue mInstance; 
private static Context mCtx; 
private RequestQueue mRequestQueue; 
private ImageLoader mImageLoader; 


private CustomVolleyRequestQueue(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 CustomVolleyRequestQueue getInstance(Context context) { 
    if (mInstance == null) { 
     mInstance = new CustomVolleyRequestQueue(context); 
    } 
    return mInstance; 
} 

public RequestQueue getRequestQueue() { 
    if (mRequestQueue == null) { 
     Cache cache = new DiskBasedCache(mCtx.getCacheDir(), 10 * 1024 * 1024); 
     Network network = new BasicNetwork(new HurlStack()); 
     mRequestQueue = new RequestQueue(cache, network); 
     // Don't forget to start the volley request queue 
     mRequestQueue.start(); 
    } 
    return mRequestQueue; 
} 

public ImageLoader getImageLoader() { 
    return mImageLoader; 
} 

}

公共类CreateNotificationActivity扩展AppCompatActivity {

private NetworkImageView mNetworkImageView; 
private ImageLoader mImageLoader; 
Bitmap bit; 

String url = "http://www.whitegadget.com/attachments/pc-wallpapers/145032d1392006652-nature-wallpaper-nature-picture.jpg"; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_create_notification); 
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    mNetworkImageView = (NetworkImageView) findViewById(R.id.networkImageView1); 

    //bit = BitmapFactory.decodeResource(getResources(), R.drawable.bgd); 

    //Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bgd); 

    mImageLoader = CustomVolleyRequestQueue.getInstance(this.getApplicationContext()) 
      .getImageLoader(); 

    Bitmap bitmap =mImageLoader.get(url, ImageLoader.getImageListener(mNetworkImageView, 
      R.mipmap.ic_launcher, R.drawable.icon)).getBitmap(); 



    Button btn = (Button) findViewById(R.id.button1); 
    btn.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      createNotification(); 
     } 
    }); 


} 

@Override 
protected void onStart() { 
    super.onStart(); 

    mImageLoader = CustomVolleyRequestQueue.getInstance(this.getApplicationContext()) 
      .getImageLoader(); 

    bit =mImageLoader.get(url, ImageLoader.getImageListener(mNetworkImageView, 
      R.mipmap.ic_launcher, R.drawable.icon)).getBitmap(); 


} 

@TargetApi(Build.VERSION_CODES.JELLY_BEAN) 
public void createNotification() 
{ 



    bit = BitmapFactory.decodeResource(getResources(), R.drawable.bgd); 

    mImageLoader.get(url, new ImageLoader.ImageListener() { 
     @Override 
     public void onResponse(ImageLoader.ImageContainer imageContainer, boolean b) { 
      bit =mImageLoader.get(url, ImageLoader.getImageListener(mNetworkImageView, 
        R.mipmap.ic_launcher, android.R.drawable 
          .ic_dialog_alert)).getBitmap(); 

      Intent intent = new Intent(getApplicationContext(),NotificationReceiverActivity.class); 
      PendingIntent pIntent = PendingIntent.getActivity(getApplicationContext(), (int) System.currentTimeMillis(), intent, 0); 


      Notification noti = new Notification.Builder(getApplicationContext()) 
        .setContentTitle("Mail Notification") 
        .setContentText("5 new unread messages") 
        .setSmallIcon(R.drawable.bgd) 
        .setStyle(new Notification.BigPictureStyle().bigPicture(bit)) 
        .setAutoCancel(true) 
        .setContentIntent(pIntent).build(); 


      NotificationManager notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); 

      noti.flags |= Notification.FLAG_AUTO_CANCEL; 

      notificationManager.notify(0,noti); 

      Log.i("LOG>>>>>>>>>>>>>>>>>>>","inside the on response"); 
     } 

     @Override 
     public void onErrorResponse(VolleyError volleyError) { 
      bit = BitmapFactory.decodeResource(getResources(), R.drawable.bgd); 
      Log.i("LOG>>>>>>>>>>>>>>>>>>>","inside the errror response"); 


     } 
    }); 

//位= mImageLoader.get(URL,ImageLoader.getImageListener(mNetworkImageView, // R.mipmap.ic_launcher,android.R.drawable // .ic_dialog_alert))getBitmap();

} 

}

相关问题