Java 使用AsyncTask将图像加载到ListView的适配器中
您好,所以我的列表视图有问题,其中包含歌曲和AlbumArt,我想做一个异步任务来获取背景中的AlbumArt 把它要么给我一个空指针,要么相册艺术是空白的,请帮助 ImageLoader.javaJava 使用AsyncTask将图像加载到ListView的适配器中,java,android,listview,android-asynctask,Java,Android,Listview,Android Asynctask,您好,所以我的列表视图有问题,其中包含歌曲和AlbumArt,我想做一个异步任务来获取背景中的AlbumArt 把它要么给我一个空指针,要么相册艺术是空白的,请帮助 ImageLoader.java public class ImageLoader extends AsyncTask<Object, String, Bitmap> { private View view; private Bitmap bitmap = null; public static BitmapDrawa
public class ImageLoader extends AsyncTask<Object, String, Bitmap> {
private View view;
private Bitmap bitmap = null;
public static BitmapDrawable drawable = null;
Context context;
Cursor cursor;
long albumId = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
@Override
protected Bitmap doInBackground(Object... parameters) {
// Get the passed arguments here
final Uri ART_CONTENT_URI = Uri.parse("content://media/external/audio/albumart");
Uri albumArtUri = ContentUris.withAppendedId(ART_CONTENT_URI, albumId);
ContentResolver res = context.getContentResolver();
InputStream in;
try {
if(bitmap != null)
{
bitmap = null;
if(drawable != null)
{
drawable = null;
}
}
in = res.openInputStream(albumArtUri);
bitmap = BitmapFactory.decodeStream(in);
Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, 1280, 720, false);
// bitmap = MediaStore.Images.Media.getBitmap(context.getContentResolver(), albumArtUri);
drawable = new BitmapDrawable(context.getResources(), resizedBitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
drawable = (BitmapDrawable) context.getResources().getDrawable(R.drawable.default_artwork);
};
return bitmap;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
if (bitmap != null && view != null) {
ImageView albumArt = (ImageView) view.getTag(R.id.iconlist);
albumArt.setImageBitmap(bitmap);
}
}
}
公共类ImageLoader扩展异步任务{
私人视野;
私有位图=空;
公共静态BitmapDrawable-drawable=null;
语境;
光标;
long albumId=cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
@凌驾
受保护位图doInBackground(对象…参数){
//在这里获取传递的参数
最终Uri艺术内容Uri=Uri.parse(“content://media/external/audio/albumart");
Uri albumArtUri=ContentUris.withAppendedId(艺术内容Uri,albumId);
ContentResolver res=context.getContentResolver();
输入流输入;
试一试{
if(位图!=null)
{
位图=空;
如果(可绘制!=null)
{
drawable=null;
}
}
in=res.openInputStream(albumArtUri);
位图=BitmapFactory.decodeStream(in);
Bitmap resizedBitmap=Bitmap.createScaledBitmap(位图,1280720,false);
//位图=MediaStore.Images.Media.getBitmap(context.getContentResolver(),albumArtUri);
drawable=新的BitmapDrawable(context.getResources(),resizedBitmap);
}catch(filenotfounde异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
drawable=(BitmapDrawable)context.getResources().getDrawable(R.drawable.default_);
};
返回位图;
}
@凌驾
受保护的void onPostExecute(位图){
if(位图!=null&&view!=null){
ImageView albumArt=(ImageView)view.getTag(R.id.iconlist);
albumArt.setImageBitmap(位图);
}
}
}
SongAdapter.java
public class SongAdapter extends CursorAdapter implements SectionIndexer{
private String mSections = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ";
private final LayoutInflater mInflater;
public SongAdapter(Context context, Cursor c, int textViewResourceId,
List<String> objects) {
super(context, c,textViewResourceId);
new ImageLoader().execute();
mInflater=LayoutInflater.from(context);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView title1 = (TextView) view.findViewById(R.id.titlelist);
TextView artist1 = (TextView) view.findViewById(R.id.artistlist);
ImageView album1 = (ImageView) view.findViewById(R.id.iconlist);
String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
long albumId = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
StringBuilder titleBuild = new StringBuilder();
titleBuild.append(title);
if(titleBuild.length() > 35)
{
titleBuild.setLength(32);
title = titleBuild.toString()+"...";
}
else
{
title = titleBuild.toString();
}
StringBuilder artistBuild = new StringBuilder();
artistBuild.append(artist);
if(artistBuild.length() > 35)
{
artistBuild.setLength(32);
artist = artistBuild.toString()+"...";
}
else
{
artist = artistBuild.toString();
}
album1.setImageDrawable(ImageLoader.drawable);
title1.setText(title);
artist1.setText(artist);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
return inflater.inflate(R.layout.rowlayout, parent, false);
}@Override
public int getPositionForSection(int section) {
// If there is no item for current section, previous section will be selected
for (int i = section; i >= 0; i--) {
for (int j = 0; j < getCount(); j++) {
if (i == 0) {
// For numeric section
for (int k = 0; k <= 9; k++) {
if (StringMatcher.match(String.valueOf(( getItem(j))), String.valueOf(k)))
return j;
}
} else {
if (StringMatcher.match(String.valueOf(getItem(j)), String.valueOf(mSections.charAt(i))))
return j;
}
}
}
return 0;
}
@Override
public int getSectionForPosition(int position) {
return 0;
}
@Override
public Object[] getSections() {
String[] sections = new String[mSections.length()];
for (int i = 0; i < mSections.length(); i++)
sections[i] = String.valueOf(mSections.charAt(i));
return sections;
}
}
公共类SongAdapter扩展游标适配器实现SectionIndexer{
私有字符串mSections=“#abcdefghijklmnopqrstuvxyz”;
私人最终布局平面图;
公共SongAdapter(上下文上下文、光标c、int textViewResourceId、,
列出对象){
super(上下文、c、textViewResourceId);
新建ImageLoader().execute();
mInflater=LayoutInflater.from(上下文);
}
@凌驾
公共void bindView(视图、上下文上下文、光标){
TextView title1=(TextView)view.findViewById(R.id.titlelist);
TextView artist1=(TextView)view.findViewById(R.id.artistlist);
ImageView album1=(ImageView)view.findViewById(R.id.iconlist);
String title=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.title));
stringartist=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.artist));
stringalbum=cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.album));
long albumId=cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
StringBuilder titleBuild=新建StringBuilder();
titleBuild.append(标题);
如果(标题build.length()>35)
{
标题建筑设置长度(32);
title=titleBuild.toString()+“…”;
}
其他的
{
title=titleBuild.toString();
}
StringBuilder artistBuild=新建StringBuilder();
artistBuild.append(艺术家);
如果(artistBuild.length()>35)
{
artistBuild.setLength(32);
artist=artistBuild.toString()+“…”;
}
其他的
{
artist=artistBuild.toString();
}
album1.setImageDrawable(ImageLoader.drawable);
标题1.setText(标题);
艺人1.setText(艺人);
}
@凌驾
公共视图newView(上下文上下文、光标、视图组父对象){
//TODO自动生成的方法存根
LayoutInflater充气器=(LayoutInflater)context.getSystemService
(上下文、布局、充气机和服务);
返回充气器。充气(R.layout.rowlayout,父级,false);
}@凌驾
公共int getPositionForSection(int段){
//如果当前节没有项目,将选择上一节
对于(int i=section;i>=0;i--){
对于(int j=0;j 对于(int k=0;k异步任务中的视图对象从未初始化,至少我看不到该代码。我认为您可以为每个“新”启动一个新的异步任务您正在适配器中创建的视图。您需要使异步任务具有对要填充的imageview的引用。一种方法如下
public class ImageLoader extends AsyncTask<Object, String, Bitmap> {
private WeakReference<ImageView> mReference;
private View view;
private Bitmap bitmap = null;
public static BitmapDrawable drawable = null;
Context context;
Cursor cursor;
long albumId = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
public ImageLoader(ImageView imageView) {
mReference = new WeakReference<ImageView>(imageView);
}
@Override
protected Bitmap doInBackground(Object... parameters) { ... your code }
@Override
protected Void onPostExecute(Bitmap bitmap) {
if(mReference != null) {
if(bitmap != null) {
ImageView view = mReference.get();
// note that this could still return null if the view or the reference has been
// garbage collected which it could be since it is a weak reference, so you should
// always check the status in this case.
//do what you want with the image view.
}
}
}
公共类ImageLoader扩展异步任务{
私人财富参考;
私人视野;
私有位图=空;
公共静态BitmapDrawable-drawable=null;
语境;
光标;
long albumId=cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
公共图像加载器(ImageView){
mrreference=新的WeakReference(图像视图);
}
@凌驾
受保护位图doInBackground(对象…参数){…您的代码}
@凌驾
受保护的Void onPostExecute(位图){
if(mrreference!=null){
if(位图!=null){
ImageView=mReference.get();
//请注意,如果视图或引用已被删除,则仍可能返回null
//垃圾被收集,因为它是一个弱引用,所以您应该
//在这种情况下,请始终检查状态。
//使用图像视图执行所需操作。
}
}
}
然后在适配器中执行如下操作
public class SongAdapter extends CursorAdapter implements SectionIndexer{
...other code...
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView title1 = (TextView) view.findViewById(R.id.titlelist);
TextView artist1 = (TextView) view.findViewById(R.id.artistlist);
ImageView album1 = (ImageView) view.findViewById(R.id.iconlist);
String title = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
String artist = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ARTIST));
String album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM));
long albumId = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.ALBUM_ID));
StringBuilder titleBuild = new StringBuilder();
titleBuild.append(title);
if(titleBuild.length() > 35)
{
titleBuild.setLength(32);
title = titleBuild.toString()+"...";
}
else
{
title = titleBuild.toString();
}
StringBuilder artistBuild = new StringBuilder();
artistBuild.append(artist);
if(artistBuild.length() > 35)
{
artistBuild.setLength(32);
artist = artistBuild.toString()+"...";
}
else
{
artist = artistBuild.toString();
}
<---->
// new code
new ImageLoader(album1).execute();
// old code album1.setImageDrawable(ImageLoader.drawable);
title1.setText(title);
artist1.setText(artist);
}
}
公共类SongAdapter扩展游标适配器实现SectionIndexer{
…其他代码。。。
@凌驾
公共void bindView(视图、上下文上下文、光标){
TextView title1=(TextView)view.findViewById(R.id.titlelist);
TextView artist1=(TextView)view.findViewById(R.id.artistlist);
阿布图像视图