Home  >  Q&A  >  body text

android - 安卓怎么判断通过图库选择的图片类型?

在调用图库选图片后图库给的图片路径的content://的没有后缀名如何判断图片是png还是gif?如果有通过bitmap判断的方法也行!

PHP中文网PHP中文网2741 days ago498

reply all(4)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 17:36:23

    You can get the name according to the image path, and then use "." to segment the words

    reply
    0
  • PHPz

    PHPz2017-04-17 17:36:23

    Determine the image type through the file header identifier, reference: http://zjf30366.blog.163.com/...

    reply
    0
  • PHPz

    PHPz2017-04-17 17:36:23

    Acquired through file header information

    /**
     * 获取文件类型
     * @param filePath
     * @return
     */
    public static String getImageType(String filePath) {
        FileInputStream is = null;
        String value = null;
        try {
            is = new FileInputStream(filePath);
            byte[] b = new byte[3];
            is.read(b, 0, b.length);
            value = bytesToHexString(b);
        } catch (Exception e) {
        } finally {
            if(null != is) {
                try {
                    is.close();
                } catch (IOException e) {}
            }
        }
        if("FFD8FF".equals(value)){
            return "jpg";
        } else if("FFD8FF".equals(value)){
            return "jpg";
        } else if("47494638".equals(value)){
            return "gif";
        } else if("424D".equals(value)){
            return "bmp";
        }
        return value;
    }
    private static String bytesToHexString(byte[] src){
        StringBuilder builder = new StringBuilder();
        if (src == null || src.length <= 0) {
            return null;
        }
        String hv;
        for (int i = 0; i < src.length; i++) {
            hv = Integer.toHexString(src[i] & 0xFF).toUpperCase();
            if (hv.length() < 2) {
                builder.append(0);
            }
            builder.append(hv);
        }
        return builder.toString();
    }

    // mFileTypes.put("FFD8FF", "jpg");
    // mFileTypes.put("89504E47", "png");
    // mFileTypes.put("47494638", "gif");
    / / mFileTypes.put("49492A00", "tif");
    // mFileTypes.put("424D", "bmp");
    // //
    // mFileTypes.put("41433130", "dwg") ; //CAD
    // mFileTypes.put("38425053", "psd");
    // mFileTypes.put("7B5C727466", "rtf"); //Diary
    // mFileTypes.put("3C3F786D6C" , "xml");
    // mFileTypes.put("68746D6C3E", "html");
    // mFileTypes.put("44656C69766572792D646174653A", "eml"); //Mail
    // mFileTypes.put("D0CF11E0 ", "doc");
    // mFileTypes.put("5374616E64617264204A", "mdb");
    // mFileTypes.put("252150532D41646F6265", "ps");
    // mFileTypes.put("255044462D312E" , "pdf");
    // mFileTypes.put("504B0304", "zip");
    // mFileTypes.put("52617221", "rar");
    // mFileTypes.put("57415645", "wav ");
    // mFileTypes.put("41564920", "avi");
    // mFileTypes.put("2E524D46", "rm");
    // mFileTypes.put("000001BA", "mpg") ;
    // mFileTypes.put("000001B3", "mpg");
    // mFileTypes.put("6D6F6F76", "mov");
    // mFileTypes.put("3026B2758E66CF11", "asf");
    // mFileTypes.put("4D546864", "mid");
    // mFileTypes.put("1F8B08", "gz");
    // mFileTypes.put("", "");
    // mFileTypes. put("", "");

    reply
    0
  • 高洛峰

    高洛峰2017-04-17 17:36:23

    Getting through the file header is a bit troublesome. The best way is to get the file name and intercept its suffix to determine the type. This is how the Windows system determines the file type, so there will be no problem in doing so.

    reply
    0
  • Cancelreply