安卓开发获取SDCard中某个目录下图片

来源:互联网 发布:c语言重要的库函数 编辑:程序博客网 时间:2024/03/28 18:06

这个函数是通用的,只要提供路径就可以查询出该目录下所有图片的路径信息,并保存到一个List<String>中。

 1.获取SDCard中某个目录下图片路径集合
 public List<String> getPictures(final String strPath) { 
    List<String> list = new ArrayList<String>(); 
    File file = new File(strPath); 
    File[] allfiles = file.listFiles(); 
    if (allfiles == null) { 
      return null; 
    } 
    for(int k = 0; k < allfiles.length; k++) { 
      final File fi = files[i]; 
      if(fi.isFile()) { 
              int idx = fi.getPath().lastIndexOf("."); 
              if (idx <= 0) { 
                  continue; 
              } 
              String suffix = fi.getPath().substring(idx); 
              if (suffix.toLowerCase().equals(".jpg") || 
                  suffix.toLowerCase().equals(".jpeg") || 
                  suffix.toLowerCase().equals(".bmp") || 
                  suffix.toLowerCase().equals(".png") || 
                  suffix.toLowerCase().equals(".gif") ) { 
                  list.add(fi.getPath()); 
              } 
      } 
   } 
   return list; 
 }

 2.获取sd卡下的图片并显示
 List<String> list = getPictures(Environment.getExternalStorageDirectory() + ""); 
 if (list != null) { 
    Log.d(TAG, "list.size = " + list.size()); 
    for (int i = 0; i < list.size(); i++) { 
        Bitmap bm = BitmapFactory.decodeFile(list.get(i)); 
        int top = 30; 
        if (i > 0) { 
            top += BitmapFactory.decodeFile(list.get(i - 1)).getHeight() + 2; 
        } 
        canvas.drawBitmap(bm, 0, top, paint); 
    } 
 } 
 else { 
    Log.d(TAG, "list is null!!!"); 
 }

    本文提供两个函数,第一个函数主要是获取SDCard中某目录下的所有图片,第二个函数主要是显示图片

用下面这种方式能实现查询实现查询sd卡某一个子目录下的图片文件详细信息 :

//selection: 指定查询条件String selection = MediaStore.Images.Media.DATA + " like %?";//设定查询目录String path="/mnt/sdcard/youpicpath";//定义selectionArgs:String[] selectionArgs = {path+"%"};c = this.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null,                                selection, selectionArgs, null);

其实原理就是改变了下查询语句,在查询条件中增加了MediaStore.Images.Media.DATA字段的限制条件,必须是和指定目录能匹配的才被查询,注意selectionselectionArgs参数是配合使用的。


ImageView是Android程序中经常用到的组件,它将一个图片显示到屏幕上。
在UI xml定义一个ImageView如下:
public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.myimage);
     ImageView image1 = (ImageView) findViewById(R.myImage.image);
     //Bitmap bitmap = getLoacalBitmap("/aa/aa.jpg"); //从本地取图片
     Bitmap bitmap =
getHttpBitmap("http://blog.3gstdy.com/wp-content/themes/twentyten/images/headers/path.jpg");
//从网上取图片
     image1 .setImageBitmap(bitmap);//设置Bitmap
}
/**
* 加载本地图片
* http://bbs.3gstdy.com
* @param url
* @return
*/
public static Bitmap getLoacalBitmap(String url) {
     try {
          FileInputStream fis = new FileInputStream(url);
          return BitmapFactory.decodeStream(fis);
     } catch (FileNotFoundException e) {
          e.printStackTrace();
          return null;
     }
}
/**
* 从服务器取图片
*http://bbs.3gstdy.com
* @param url
* @return
*/
public static Bitmap getHttpBitmap(String url) {
     URL myFileUrl = null;
     Bitmap bitmap = null;
     try {
          Log.d(TAG, url);
          myFileUrl = new URL(url);
     } catch (MalformedURLException e) {
          e.printStackTrace();
     }
     try {
          HttpURLConnection conn = (HttpURLConnection) myFileUrl.openConnection();
          conn.setConnectTimeout(0);
          conn.setDoInput(true);
          conn.connect();
          InputStream is = conn.getInputStream();
          bitmap = BitmapFactory.decodeStream(is);
          is.close();
     } catch (IOException e) {
          e.printStackTrace();
     }
     return bitmap;
}

0 0
原创粉丝点击