您的位置:首页 > 移动开发 > Android开发

Android 获取手机图片

2012-04-27 14:39 232 查看
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;

            }

            

        }

    }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐