第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在上傳到服務(wù)器之前減小圖像文件大小

如何在上傳到服務(wù)器之前減小圖像文件大小

梵蒂岡之花 2019-08-31 15:21:19
許多應(yīng)用程序允許共享從圖庫中挑選的圖像。他們上傳原始圖片文件嗎?這是1-3 MB?或者他們處理?在任何情況下,我如何從文件路徑中獲取圖像,通過降低分辨率來減小其大小,并將其保存在其他地方并嘗試上傳?我試過了:Bitmap photo = decodeSampledBitmapFromFile(filePath, DESIRED_WIDTH,                    DESIRED_HEIGHT);FileOutputStream out = new FileOutputStream(filePath);photo.compress(Bitmap.CompressFormat.JPEG, 100, out);public static Bitmap decodeSampledBitmapFromFile(String path, int reqWidth,        int reqHeight) {    final BitmapFactory.Options options = new BitmapFactory.Options();    options.inJustDecodeBounds = true;    BitmapFactory.decodeFile(path, options);    final int height = options.outHeight;    final int width = options.outWidth;    options.inPreferredConfig = Bitmap.Config.ARGB_8888;    int inSampleSize = 1;    if (height > reqHeight) {        inSampleSize = Math.round((float) height / (float) reqHeight);    }    int expectedWidth = width / inSampleSize;    if (expectedWidth > reqWidth) {        inSampleSize = Math.round((float) width / (float) reqWidth);    }    options.inSampleSize = inSampleSize;    options.inJustDecodeBounds = false;    return BitmapFactory.decodeFile(path, options);}但這是他們正確的方法嗎?因為我在這里看到了答案compression operation takes rather big amount of time
查看完整描述

3 回答

?
鳳凰求蠱

TA貢獻1825條經(jīng)驗 獲得超4個贊

我使用此功能在上傳之前減小圖像的大小,它將圖像大小減小到接近200 KB并保持質(zhì)量相對較好,您可以通過更改REQUIRED_SIZE和inSampleSize來修改它以實現(xiàn)您的目的:


public File saveBitmapToFile(File file){

    try {


        // BitmapFactory options to downsize the image

        BitmapFactory.Options o = new BitmapFactory.Options();

        o.inJustDecodeBounds = true;

        o.inSampleSize = 6;

        // factor of downsizing the image


        FileInputStream inputStream = new FileInputStream(file);

        //Bitmap selectedBitmap = null;

        BitmapFactory.decodeStream(inputStream, null, o);

        inputStream.close();


        // The new size we want to scale to

        final int REQUIRED_SIZE=75;


        // Find the correct scale value. It should be the power of 2.

        int scale = 1;

        while(o.outWidth / scale / 2 >= REQUIRED_SIZE &&

                        o.outHeight / scale / 2 >= REQUIRED_SIZE) {

            scale *= 2;

        }


        BitmapFactory.Options o2 = new BitmapFactory.Options();

        o2.inSampleSize = scale;

        inputStream = new FileInputStream(file);


        Bitmap selectedBitmap = BitmapFactory.decodeStream(inputStream, null, o2);

        inputStream.close();


        // here i override the original image file

        file.createNewFile();

        FileOutputStream outputStream = new FileOutputStream(file);


        selectedBitmap.compress(Bitmap.CompressFormat.JPEG, 100 , outputStream);


        return file;

    } catch (Exception e) {

        return null;

    }

}

注意:我在此函數(shù)中調(diào)整大小并覆蓋原始文件圖像,您也可以將其寫入另一個文件。


我希望它可以幫助你。


查看完整回答
反對 回復(fù) 2019-08-31
?
GCT1015

TA貢獻1827條經(jīng)驗 獲得超4個贊

這是我的解決方案


/*

* This procedure will replace the original image

* So you need to do a tmp copy to send before reduce

*/

public static boolean reduceImage(String path, long maxSize) {

    File img = new File(path);

    boolean result = false;

    BitmapFactory.Options options = new BitmapFactory.Options();

    Bitmap bitmap = null;

    options.inSampleSize=1;

    while (img.length()>maxSize) {

        options.inSampleSize = options.inSampleSize+1;

        bitmap = BitmapFactory.decodeFile(path, options);

        img.delete();

        try

            {

                FileOutputStream fos = new FileOutputStream(path);

                img.compress(path.toLowerCase().endsWith("png")?

                                Bitmap.CompressFormat.PNG:

                                Bitmap.CompressFormat.JPEG, 100, fos);

                fos.close();

                result = true;

             }catch (Exception errVar) { 

                errVar.printStackTrace(); 

             }

    };

    return result;

}

編輯刪除了其他程序調(diào)用


查看完整回答
反對 回復(fù) 2019-08-31
  • 3 回答
  • 0 關(guān)注
  • 509 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號