android 網絡加載圖片並保存本地,壓縮,jpeg png格式分析

 標題起的有點長,主要是不知道該怎麼表達,android的圖片獲取和保存相信很多人都做過.有時候我們在對圖片保存時會將圖片壓縮一下,也就是這個壓縮,
 會造成很多問題.通常情況下,網絡獲取圖片均爲jpeg格式,而有些時候app爲了達到某些效果,會給出png的透明邊圖片,可是我們在壓縮的時候依然用jpeg的
  格式進行的壓縮.這就是問題的根源.但是我們也不可能再寫一套保存邏輯用png的格式,再說了,我們也不知道哪個圖是png格式,哪個圖是jpeg格式,對不對?
  以前做項目的時候遇到過,解決了.也就沒再關心過這個東西.今天突然想到一些問題,就在網上搜索有沒有更好的解決方式,可是一個都沒搜到,連解決這種保存方式的
  帖子都沒有,這頁促使我把我以前的解決方式分享給大家.有更好的方式大家可以互相討論.具體請看代碼:
if(is!=null){//is就是網上獲取的圖片流
        // 配置bitmap,防止內存溢出
        BitmapFactory.Options options = new BitmapFactory.Options();
        options .inPreferredConfig = Bitmap.Config.RGB_565;
        options .inPurgeable = true;
        options .inInputShareable = true;
//options.inJustDecodeBounds = false;這句必須爲false 也可以不寫,默認是false,如果改成true返回的bitmap是空,主要是返回圖片的一些尺寸信息.所以請寫成false,這樣返回的就是bitmap.如果有疑問可以百度一下,很多帖子寫的很清楚.
        bitmap= BitmapFactory.decodeStream(is, null , options );
        is.close();
        UtilFile.saveImgToCompletePath(bitmap, path,      
        UtilImage.getImgFormat(options));  
    }


//UtilImage類
//這個方法就是動態的根據圖片類型選擇壓縮的圖片格式,主要根據options的信息來判斷圖片的後綴,這樣就得到了圖片的類型
public static CompressFormat getImgFormat(BitmapFactory.Options options){
        String type = options.outMimeType;
        if(type!=null&&type.indexOf("png")>-1) return CompressFormat.PNG;
                else return CompressFormat.JPEG;
    }


//UtilFIle類
/**
     * 保存圖片到sd卡
     * @param bitmap
     * @param completePath : 完整路徑
     * @param compressFormat
     */
public static void saveImgToCompletePath(Bitmap bitmap,String 
    completePath,CompressFormat format) {
        File file = new File(completePath);
        File parentFile = file.getParentFile();
        if (!parentFile.exists())
            parentFile.mkdirs();
        if (bitmap != null) {
            try{
                FileOutputStream fos = new FileOutputStream(file);
                bitmap.compress(format, 100, fos);
            }catch(Exception e){
                InputStream is = UtilImage.bitmapToInputStream(bitmap, 
                format,0);
                saveFileToCompletePath(getSDDir() + 
                file.getAbsolutePath(), is, false);
            }
        }
    }


//UtilFile 類
/**
     * 在SD卡上存文件
     * @param completePath : 完整路徑
     * @param is
     * @return 成功file,失敗null
     */
    public static File saveFileToCompletePath(String completePath, InputStream is, boolean append) {
        File file = new File(completePath);
        File parentFile = file.getParentFile();
        if (!parentFile.exists())
            parentFile.mkdirs();
        if (is != null) {
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(file, append);
                byte[] b = new byte[1024];
                int len = -1;
                while ((len = is.read(b)) != -1) {
                    fos.write(b, 0, len);
                }
                fos.close();
                is.close();
                return file;
            } catch (Exception e) {
                e.printStackTrace();
//              LogManager.reportError("寫sd文件異常",e);
                return null;
            }
        } else
            return null;
    }
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章