为什么传递过去的图片的URI如果有为空的时候程序会崩掉
是从网上获取到的接口,这个接口的图片地址有一两个是为空的,为什么传递过去的图片的URI如果有为空的时候程序会崩掉,调用public void showImageByAsyncTask(ImageView imageView, String Url)方法就会报java.lang.RuntimeException: An error occured while executing doInBackground()异常,图片的地址有的话就能正常显示
package com.example.mediaplayer;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.HashSet;
import java.util.Set;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.support.v4.util.LruCache;
import android.widget.ImageView;
import android.widget.ListView;
public class lvImageLoader {
private ImageView mImageView;
private String murl;
private ListView mListView;
private Set<NewsAsyncTask> mTask;
// 创建缓存机制 Cache
private LruCache<String, Bitmap> mLruCache;
public lvImageLoader(ListView listView) {
mListView = listView;
mTask = new HashSet<NewsAsyncTask>();
// 获得当前最大可用内存
int maxMemroy = (int) Runtime.getRuntime().maxMemory();
// 设置最大缓存大小
int cacheSize = maxMemroy / 4;
mLruCache = new LruCache<String, Bitmap>(cacheSize) {
// 在每次加入内存缓存时候调用
protected int sizeOf(String key, Bitmap value) {
// 在每次存入缓存的时候给予bitmap准确的大小
return value.getByteCount();
};
};
}
/**
* 增加缓存
*
* @param url
* @param bitmap
*/
public void addBitmapToCache(String url, Bitmap bitmap) {
if (getBitmapToCache(url) == null) {
mLruCache.put(url, bitmap);
}
}
public Bitmap getBitmapToCache(String url) {
return mLruCache.get(url);
}
private Handler handler = new Handler() {
public void handleMessage(android.os.Message msg) {
if (mImageView.getTag().equals(murl)) {
mImageView.setImageBitmap((Bitmap) msg.obj);
}
};
};
/**
* 使用多线程加载图片
*
* @param image
* @param url
*/
public void ShowImageByThread(ImageView image, final String url) {
mImageView = image;
murl = url;
new Thread() {
@Override
public void run() {
super.run();
Bitmap bitmap = getBitMapFromURL(url);
Message message = Message.obtain();
message.obj = bitmap;
handler.sendMessage(message);
}
}.start();
}
/**
* 使用httpUrlConnection下载Bitmap
*
* @param urlString
* @return
*/
public Bitmap getBitMapFromURL(String urlString) {
Bitmap bitmap;
InputStream is = null;
try {
URL url = new URL(urlString);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
is = new BufferedInputStream(connection.getInputStream());
bitmap = BitmapFactory.decodeStream(is);
connection.disconnect();
return bitmap;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
/**
* 用来加载start到end的图片
*
* @param Start
* @param End
*/
public void loadImages(int Start, int End) {
for (int i = Start; i < End; i++) {
String url = AlbumAdapte.URLS[i];
Bitmap bitmap = getBitmapToCache(url);
if (bitmap == null) {
NewsAsyncTask Task = new NewsAsyncTask(url);
Task.execute(url);
mTask.add(Task);
} else {
ImageView imageView = (ImageView) mListView
.findViewWithTag(url);
}
}
};
/**
* 使用异步任务加载Bitmap
*
* @param imageView
* @param Url
*/
public void showImageByAsyncTask(ImageView imageView, String Url) {
Bitmap bitmap = getBitmapToCache(Url);
// 在这里使用loadImages来加载图片,bitmap为空时不再调用AsynTask而是加在一张默认图片
if (bitmap == null) {
imageView.setImageResource(R.drawable.ic_launcher);
} else {
imageView.setImageBitmap(bitmap);
}
// if (bitmap == null) {
// new NewsAsyncTask(Url).execute(Url);
// } else {
// imageView.setImageBitmap(bitmap);
// }
}
private class NewsAsyncTask extends AsyncTask<String, Void, Bitmap> {
// private ImageView mImageView;
private String mUrl;
public NewsAsyncTask(String mUrl) {
// this.mImageView = mImageView;
this.mUrl = mUrl;
}
@Override
protected Bitmap doInBackground(String... params) {
// 从网络获取图片or添加缓存
String url = params[0];
// 从网络中获取中取出图片
Bitmap bitmap = getBitMapFromURL(url);
if (bitmap != null) {
addBitmapToCache(url, bitmap);
}
return bitmap;
}
@Override
protected void onPostExecute(Bitmap result) {
super.onPostExecute(result);
/*
* if (mImageView.getTag().equals(mUrl)) {
* mImageView.setImageBitmap(result); }
*/
ImageView imageView = (ImageView) mListView.findViewWithTag(mUrl);
if (imageView != null && result != null) {
imageView.setImageBitmap(result);
}
}
}
public void cancelAllTasks() {
if (mTask != null) {
for (NewsAsyncTask task : mTask) {
task.cancel(false);
}
}
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////////
package com.example.mediaplayer;
import java.util.List;
import com.ximalaya.ting.android.opensdk.model.album.Album;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
public class AlbumAdapte extends BaseAdapter implements OnScrollListener{
private List<Album> mList;
private LayoutInflater mInflater;
private lvImageLoader mImageLoad;
private int mStart,mEnd;
public static String[] URLS;
private boolean mFirstIn;
public AlbumAdapte(Context context, List<Album> mList,ListView listView){
this.mList = mList;
mFirstIn = true;
mInflater = LayoutInflater.from(context);
mImageLoad = new lvImageLoader(listView);
URLS = new String[mList.size()];
for (int i=0;i<mList.size();i++){
URLS[i] = mList.get(i).getCoverUrlSmall();
}
listView.setOnScrollListener(this);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mList.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return mList.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int arg0, View convertView, ViewGroup arg2) {
ViewHolder viewHolder = new ViewHolder();
if (convertView == null){
convertView = mInflater.inflate(R.layout.albuminformationitem, null);
viewHolder.tvIcon = (ImageView) convertView.findViewById(R.id.lv_imageCoverUrlSmall);
viewHolder.tvTitle = (TextView) convertView.findViewById(R.id.lv_AlbumTitle);
viewHolder.tvContent = (TextView) convertView.findViewById(R.id.lv_AlbumIntro);
convertView.setTag(viewHolder);
}else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.tvIcon.setImageResource(R.drawable.ic_launcher);
String url = mList.get(arg0).getCoverUrlSmall();
viewHolder.tvIcon.setTag(url);
// new ImageLoad().showImageByThread(viewHolder.tvIcon, url);
mImageLoad.showImageByAsyncTask(viewHolder.tvIcon, url);
viewHolder.tvTitle.setText(mList.get(arg0).getAlbumTitle());
// viewHolder.tvTitle.setText(mList.get(arg0).newsIconUrl);
viewHolder.tvContent.setText(mList.get(arg0).getAlbumIntro());
return convertView;
}
class ViewHolder {
TextView tvTitle,tvContent;
ImageView tvIcon;
}
@Override
public void onScroll(AbsListView view, int firstVisiableItem, int visiableItemCount, int totalItem) {
mStart = firstVisiableItem;
mEnd = mStart + visiableItemCount;
// 首次加载调用
if(mFirstIn&&visiableItemCount>0){
mImageLoad.loadImages(mStart, mEnd);
mFirstIn = false;
}
}
@Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
if(scrollState == SCROLL_STATE_IDLE){
// 加载可见性
mImageLoad.loadImages(mStart, mEnd);
} else {
// 停止所有的ansyncTask;
mImageLoad.cancelAllTasks();
}
}
}