2014-03-27 160 views
0

在此之後link我設法顯示來自可繪製文件夾的圖像。但是,我想在此文件夾中顯示,它位於SD卡的圖像,例如:在GridView中顯示存儲在SD卡中的圖像

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).toString() + File.Seperator + "MyApp" 

但我不能夠改變我的代碼SD卡來從一個文件夾的圖像,並在顯示它網格視圖。

我從繪製文件夾顯示圖像代碼:

PictureGallery.java

public class PictureGallery extends Activity { 

    protected static final String EXTRA_RES_ID = "POS"; 

    private ArrayList<Integer> mThumbIdsFlowers = new ArrayList<Integer>(
     Arrays.asList(R.drawable.ic_launcher, R.drawable.contrast, 
       R.drawable.saltpepper1, R.drawable.saltpepper2, 
       R.drawable.ic_launcher, R.drawable.contrast, 
       R.drawable.saltpepper1, R.drawable.saltpepper2, 
       R.drawable.ic_launcher, R.drawable.contrast, 
       R.drawable.saltpepper1, R.drawable.saltpepper2)); 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.picture_gallery); 

     GridView gridview = (GridView) findViewById(R.id.gridview); 

     gridview.setAdapter(new ImageAdapter(this, mThumbIdsFlowers)); 
    } 
} 

ImageAdapter.java

public class ImageAdapter extends BaseAdapter { 
    private static final int PADDING = 8; 
    private static final int WIDTH = 250; 
    private static final int HEIGHT = 250; 
    private Context mContext; 
    private List<Integer> mThumbIds; 

    public ImageAdapter(Context c, List<Integer> ids) { 
     mContext = c; 
     this.mThumbIds = ids; 
    } 

    @Override 
    public int getCount() { 
     return mThumbIds.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return null; 
    } 

    // Will get called to provide the ID that 
    // is passed to OnItemClickListener.onItemClick() 
    @Override 
    public long getItemId(int position) { 
     return mThumbIds.get(position); 
    } 

    // create a new ImageView for each item referenced by the Adapter 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     ImageView imageView = (ImageView) convertView; 

     // if convertView's not recycled, initialize some attributes 
     if (imageView == null) { 
      imageView = new ImageView(mContext); 
      imageView.setLayoutParams(new GridView.LayoutParams(WIDTH, HEIGHT)); 
      imageView.setPadding(PADDING, PADDING, PADDING, PADDING); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
     } 

     imageView.setImageResource(mThumbIds.get(position)); 
     return imageView; 
    } 
} 

我道歉問這樣一個業餘的問題。在我的防守中,我是一名android和java編程的新手。提前致謝。

+0

http://stackoverflow.com/questions/14892369/images-from-sd-card-to-gridview – Nezir

回答

2

PictureGallery.java

public class PictureGallery extends Activity { 

    ArrayList<String> images; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.picture_gallery); 

     images = new ArrayList<String>();// list of file paths 
     getFromSdcard(); 

     GridView gridview = (GridView) findViewById(R.id.gridview); 
     gridview.setAdapter(new ImageAdapter(this, images)); 
    } 

    File[] listFile; 

    public void getFromSdcard() 
    { 
     File file= new File(Environment.getExternalStoragePublicDirectory(
       Environment.DIRECTORY_PICTURES), "InsCam + FixBlur"); 

     if (file.isDirectory()) 
     { 
      listFile = file.listFiles(); 

      for (int i = 0; i < listFile.length; i++) 
      { 
       images.add(listFile[i].getAbsolutePath()); 
      } 
     } 
    } 
} 

ImageAdapter.java

public class ImageAdapter extends BaseAdapter { 
    private static final int PADDING = 8; 
    private static final int WIDTH = 250; 
    private static final int HEIGHT = 250; 
    private Context mContext; 
    private List<String> mThumbIds; 

    public ImageAdapter(Context c, List<String> ids){ 
     mContext = c; 
     this.mThumbIds = ids; 
    } 

    @Override 
    public int getCount() { 
     return mThumbIds.size(); 
    } 

    @Override 
    public Object getItem(int position) { 
     return null; 
    } 

    // Will get called to provide the ID that 
    // is passed to OnItemClickListener.onItemClick() 
    @Override 
    public long getItemId(int position) { 
     return position; 
    } 

    // create a new ImageView for each item referenced by the Adapter 
    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 

     ImageView imageView = (ImageView) convertView; 

     // if convertView's not recycled, initialize some attributes 
     if (imageView == null) { 
      imageView = new ImageView(mContext); 
      imageView.setLayoutParams(new GridView.LayoutParams(WIDTH, HEIGHT)); 
      imageView.setPadding(PADDING, PADDING, PADDING, PADDING); 
      imageView.setScaleType(ImageView.ScaleType.CENTER_CROP); 
     } 

     // First decode with inJustDecodeBounds=true to check dimensions 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(mThumbIds.get(position), options); 

     // Set inSampleSize 
     options.inSampleSize = 4; 

     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 
     Bitmap myBitmap = BitmapFactory.decodeFile(mThumbIds.get(position), options); 

     imageView.setImageBitmap(myBitmap); 

     return imageView; 
    } 
} 
相關問題