我有位图的arraylist,我需要在一些图像视图中打印图像。我填写arraylist加载我的手机的SD卡中的图片,我尝试打印它,但imageview没有显示任何图片。
我的活动代码如下。
public class MainActivity extends Activity{
public static int seleccionado=0;
private GridView gridview;
public static ArrayList<Bitmap> imagenes;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
imagenes=new ArrayList<Bitmap>();
String[] imgColumnID = { MediaStore.Images.Thumbnails._ID };
@SuppressWarnings("deprecation")
Cursor cursor = managedQuery(
MediaStore.Images.Media.EXTERNAL_CONTENT_URI, imgColumnID,
MediaStore.Images.Media.DATA + " like ? ",
new String[] { "%sdcard%" }, null);
if(cursor.moveToFirst()){
do{
String result=cursor.getString(0);
byte [] encodeByte=Base64.decode(result,Base64.DEFAULT);
Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
imagenes.add(bitmap);
}while(cursor.moveToNext());
}
Log.e("ima", String.valueOf(imagenes.size()));
gridview=(GridView)findViewById(R.id.gridview);
gridview.setAdapter(new AdapterImagenes(this));
gridview.setSelection(seleccionado);
}
protected void onStart(){
super.onStart();
gridview=(GridView)findViewById(R.id.gridview);
gridview.setAdapter(new AdapterImagenes(this));
gridview.setSelection(seleccionado);
}
public class AdapterImagenes extends BaseAdapter{
private Context contexto;
public AdapterImagenes(Context c){
contexto=c;
}
public int getCount(){
return imagenes.size();
}
public Object getItem(int position){
return null;
}
public long getItemId(int position){
return 0;
}
@SuppressLint("NewApi") public View getView(final int position, View convertView, ViewGroup parent){
ImageView imageView;
if(convertView==null){
imageView=new ImageView(contexto);
imageView.setLayoutParams(new GridView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 130));
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN){
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setCropToPadding(true);
}else{
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
}
imageView.setPadding(2, 2, 2, 2);
}else{
imageView=(ImageView)convertView;
}
imageView.setImageBitmap(imagenes.get(position));
imageView.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v){
seleccionado=(Integer)v.getTag();
notifyDataSetChanged();
String index=String.valueOf(position);
Bundle extras=new Bundle();
extras.putString("position", index);
startActivity(new Intent(MainActivity.this, VerFotos.class).putExtras(extras).addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
}
});
try{
imageView.setTag(position);
if(position==seleccionado){
imageView.setBackgroundColor(Color.parseColor("#ff6203"));
}else{
imageView.setBackgroundColor(Color.TRANSPARENT);
}
}catch(Exception e){
Log.e("Error", e.getMessage());
}
return imageView;
}
}
}