Android 获取手机图片

来源:互联网 发布:大数据精准营销 编辑:程序博客网 时间:2024/04/23 16:44

1.找到图片存放的路径

2.迭代此路径下是否存在后缀名与图片的后缀名相符合

3.相符合就把文件存放在list中

4.规格图片的大小

那么下面开始看代码

String path = null;

ArrayList<String> list= null;

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

      /*判断sd卡是否存在*/

        boolean sdCardExist = Environment.getExternalStorageState()
        .equals(android.os.Environment.MEDIA_MOUNTED); 

    /*获取存放的路径*/

       if(sdCardExist){

       path = Environment.getExternalStorageDirectory()+" ";

    }

    LoadImageList();

}

public static boolean LoadImageList() {
        try {
            if (path == null || path.length() == 0)
                return false;
            
            list= new ArrayList<String>();
            
            findFile(path);
            
            return true;
        } catch(Exception ex) {
            
            ex.printStackTrace();
            return false;
        }


     /*图片的迭代*/
     public static ArrayList<String> findFile(final String strPath) {  
         fileList = new ArrayList<String>();  
              
            File file = new File(strPath);  
            File[] files = file.listFiles();  
              
            if (files == null) {  
                return null;  
            }  
              
            for(int i = 0; i < files.length; i++) {  
                final File f = files[i];  
                if(f.isFile()) {  
                    try{  
                        int idx = f.getPath().lastIndexOf(".");  
                        if (idx <= 0) {  
                            continue;  
                        }  
                        String suffix = f.getPath().substring(idx);  
                        if (suffix.toLowerCase().equals(".jpg") ||  
                            suffix.toLowerCase().equals(".jpeg") ||  
                            suffix.toLowerCase().equals(".bmp") ||  
                            suffix.toLowerCase().equals(".png") ||  
                            suffix.toLowerCase().equals(".gif") )  
                        {  
                            fileList.add(f.getPath());  
                        }  
                        
                    } catch(Exception e) {  
                        e.printStackTrace();  
                    }  
                }  
            }  



               /*规格图片的大小并转换为二进制数据*/

     public static boolean loadImageFile() {
            try {
                imageData = null;
                
                File imageFile = new File(path);
                if (imageFile.exists() == false)
                    return false;
  
                BitmapFactory.Options o = new BitmapFactory.Options();
                o.inJustDecodeBounds = true;
                BitmapFactory.decodeStream(new FileInputStream(imageFile),null,o);
    
                int width_tmp=o.outWidth, height_tmp=o.outHeight;
                int scale=1;
                while(true){
                    if(width_tmp/2<width || height_tmp/2<height)
                        break;
                    width_tmp/=2;
                    height_tmp/=2;
                    scale*=2;
                }

            
                BitmapFactory.Options o2 = new BitmapFactory.Options();
                o2.inSampleSize=scale;
                
                 bitmap = BitmapFactory.decodeStream(new FileInputStream(imageFile), null, o2);
                ByteArrayOutputStream buf = new ByteArrayOutputStream();
                bitmap.compress(Bitmap.CompressFormat.PNG, 100, buf);
                buf.flush();
                
                imageData = buf.toByteArray();
                buf.close();

                 return true;
            } catch(Exception ex) {
                ex.printStackTrace();
                return false;
            }
            
        }


    }