Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/225.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用albumart优化列表视图_Android_Listview_Optimization_Bitmap - Fatal编程技术网

Android 使用albumart优化列表视图

Android 使用albumart优化列表视图,android,listview,optimization,bitmap,Android,Listview,Optimization,Bitmap,我的ListView包含所有具有唱片集艺术的歌曲,速度非常慢,而且无论我如何重新调整唱片集艺术的大小,它都会滞后。请提供帮助,以及如何获取可索引列表以处理歌曲标题。Android初学者,抱歉英语不好。 谢谢你的帮助 public class SongAdapter extends CursorAdapter implements SectionIndexer{ private String mSections = "#ABCDEFGHIJKLMNOPQRSTUVWXYZ"; private

我的ListView包含所有具有唱片集艺术的歌曲,速度非常慢,而且无论我如何重新调整唱片集艺术的大小,它都会滞后。请提供帮助,以及如何获取可索引列表以处理歌曲标题。Android初学者,抱歉英语不好。 谢谢你的帮助

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);

    mInflater=LayoutInflater.from(context);

}
 private Bitmap bitmap = null;
 private BitmapDrawable drawable = null;



@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();
      }




      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, 50, 50, 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);
            }

album1.setImageDrawable(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);
mInflater=LayoutInflater.from(上下文);
}
私有位图=空;
私有BitmapDrawable-drawable=null;
@凌驾
公共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();
}
最终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);
位图resizedBitmap=Bitmap.createScaledBitmap(位图,50,50,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_);
}
相册1.可设置图像可绘制(可绘制);
标题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,根据我的说法,问题在于:

 in = res.openInputStream(albumArtUri);
 bitmap = BitmapFactory.decodeStream(in);
 Bitmap resizedBitmap = Bitmap.createScaledBitmap(bitmap, 50, 50, false);
您应该在后台线程而不是在主线程上执行上述操作,否则它们将阻塞主(UI)线程并使所有操作都无响应

也不是直接将全分辨率位图加载到内存中,而是考虑使用<代码> BitmapFactory。选项< /代码>首先对它进行采样。< /P> 请在此处阅读更多信息:


你也可以考虑使用这个很棒的图像加载库(支持内容提供者URI):

谢谢你的帮助,我的列表是平滑的并且不再滞后。