简体   繁体   中英

Load pictures from SD card and show it in a imageview

I' trying to load all the pictures from the sdcard and show it in a imageview of my android application. I have one cursor for read the pictures and put it into an arraylist of bitmap. Now I can load all the pictures of the sdcard but when I print it into a imageview it isn't showing. Please help me if someone know how to do it.

Thank you. This is the code of the activity.

public class VerFotos extends Activity{
public static ArrayList<Bitmap> imagenes;
@Override
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    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()){
        String result=cursor.getString(0);
        byte [] encodeByte=Base64.decode(result,Base64.DEFAULT);
        Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
        imagenes.add(bitmap);
    }
    String imag=getIntent().getStringExtra("position");
    int index=Integer.parseInt(imag);
    setContentView(R.layout.fragment_ver_fotos);
    ExtendedViewPager mViewPager=(ExtendedViewPager)findViewById(R.id.image);
    mViewPager.setAdapter(new TouchImageAdapter());
    mViewPager.setCurrentItem(index);
}
static class TouchImageAdapter extends PagerAdapter {
    @Override
    public int getCount() {
        return imagenes.size();
    }

    @Override
    public View instantiateItem(ViewGroup container, int position) {
        TouchImageView img=new TouchImageView(container.getContext());
        img.setImageBitmap(imagenes.get(position));
        container.addView(img, LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
        return img;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        container.removeView((View)object);
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view==object;
    }

}
}

Instead of iterating through the results in a loop, you're only getting the first result. Try this:

while (cursor.moveToNext()) {
    String result=cursor.getString(0);
    byte [] encodeByte=Base64.decode(result,Base64.DEFAULT);
    Bitmap bitmap=BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
    imagenes.add(bitmap);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM