我正在使用内容解析器在网格视图中显示图像大拇指。
问题:它在我的设备上正常运行,有50张图片,但在我的其他设备上它会挂起。如何减少加载时间并提高应用程序速度。
我使用Base Adapter显示拇指的活动如下
public class CustomGallaryViewActivity extends Activity {
Context mContext;
ArrayList<ImageItem> imageList;
private int listCount;
private ImageAdapter imageAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_custom_gallary_view);
mContext=this;
imageList=CreateImageThumbnails.createThumbnails(CustomGallaryViewActivity.this);
listCount=imageList.size();
GridView imageGrid=(GridView)findViewById(R.id.grid_view);
imageAdapter=new ImageAdapter();
imageGrid.setAdapter(imageAdapter);
}
公共类ImageAdapter扩展了BaseAdapter {
@Override
public int getCount() {
// TODO Auto-generated method stub
return imageList.size();
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ViewHolder holder;
if(convertView==null){
holder=new ViewHolder();
LayoutInflater infalter=(LayoutInflater)mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
convertView=infalter.inflate(R.layout.galleryitem,null);
holder.imageview=(ImageView) convertView.findViewById(R.id.thumbImage);
convertView.setTag(holder);
}
else{
holder=(ViewHolder) convertView.getTag();
}
holder.imageview.setId(position);
holder.imageview.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
int id = v.getId();
Log.d("IDIDID",""+id);
ImageItem item = imageList.get(id);
Intent intent=new Intent(mContext,FullScreenImageActivtiy.class);
//intent.putExtra("ID",item.id);
intent.putExtra("ImageItemVal",new int[]{item.id,item.imgNumber,listCount,id});
startActivity(intent);
}
});
ImageItem item=new ImageItem();
item=imageList.get(position);
holder.imageview.setImageBitmap(item.img);
return convertView;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.custom_gallary_view, menu);
return true;
}
class ViewHolder {
ImageView imageview;
}
}
我正在使用以下课程
创建拇指列表public class CreateImageThumbnails{
public static ArrayList<ImageItem> createThumbnails(Activity refActivtiy){
ArrayList<ImageItem> thumbNails=new ArrayList<ImageItem>();
final String[] columns = { MediaStore.Images.Thumbnails._ID };
final String orderBy = MediaStore.Images.Media._ID;
//Cursor imageCursor=refActivtiy.managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,columns,null,null,orderBy);
Cursor imageCursor = refActivtiy.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,null, null,orderBy);
if(imageCursor!=null){
int count=imageCursor.getCount();
Log.d("Count Vlaue",""+count);
int image_column_index=imageCursor.getColumnIndex(MediaStore.Images.Media._ID);
for(int i=0;i < count;i++){
imageCursor.moveToPosition(i);
int id=imageCursor.getInt(image_column_index);
Log.d("Image ID : ",""+id);
ImageItem imageItem=new ImageItem();
imageItem.id=id;
imageItem.img = MediaStore.Images.Thumbnails.getThumbnail(
refActivtiy.getApplicationContext().getContentResolver(), id,
MediaStore.Images.Thumbnails.MICRO_KIND, null);
thumbNails.add(imageItem);
}
}
return thumbNails;
}
}
答案 0 :(得分:0)
我只是传递了这段代码返回的游标对象,问题解决了......
final String[] columns = { MediaStore.Images.Thumbnails._ID };
final String orderBy = MediaStore.Images.Media._ID;
imageCursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, columns,null, null,orderBy);