當前位置:首頁 » 安卓系統 » android壓縮上傳圖片

android壓縮上傳圖片

發布時間: 2023-04-23 08:07:05

1. Android圖片壓縮與優化的幾種方式

1.使用的圖片最好用png格式

2.如果你的圖片仍然大的話可以在項目裡面點擊右鍵選擇convert to Webp,轉成webp格式圖片可以變小很多!

2. android圖片壓縮避免OOM

簡單吹下牛:很多app都會要載入圖片,但是如果不壓縮圖片就很容易OOM,

個人看來OOM 出現原因總的來說分為兩種:

一種是內存溢出(好像在扯淡,OOM本身就是內存溢出)

另一種是:圖片過大,一個屏幕顯示不完全造成,似乎也是一。。 如有錯誤純屬扯淡;

為了避免上面的情況:載入圖片的時候可以進行壓縮,上傳的時候要可以進行壓縮,在圖片不可見的時候進行回收(onDetach()),再吹一句 用了fresco+壓縮之後載入圖片完全沒問題了。

一、質量壓縮方法:

privateBitmap compressImage(Bitmap image) {

ByteArrayOutputStream baos =newByteArrayOutputStream();

image.compress(Bitmap.CompressFormat.JPEG,100, baos);//質量壓縮方法,這里100表示不壓縮,把壓縮後的數據存放到baos中

intoptions =100;

while( baos.toByteArray().length /1024>100) {//循環判斷如果壓縮後圖片是否大於100kb,大於繼續壓縮

baos.reset();//重置baos即清空baos

image.compress(Bitmap.CompressFormat.JPEG, options, baos);//這里壓縮options%,把壓縮後的數據存放到baos中

options -=10;//每次都減少10

}

ByteArrayInputStream isBm =newByteArrayInputStream(baos.toByteArray());//把壓縮後的數據baos存放到ByteArrayInputStream中

Bitmap bitmap = BitmapFactory.decodeStream(isBm,null,null);//把ByteArrayInputStream數據生成圖片

returnbitmap;

}

二、圖片按比例大小壓縮方法(根據Bitmap圖片壓縮)

privateBitmap comp(Bitmap image) {

ByteArrayOutputStream baos =newByteArrayOutputStream();

image.compress(Bitmap.CompressFormat.JPEG,100, baos);

if( baos.toByteArray().length /1024>1024) {//判斷如果圖片大於1M,進行壓縮避免在生成圖片(BitmapFactory.decodeStream)時溢出

baos.reset();//重置baos即清空baos

image.compress(Bitmap.CompressFormat.JPEG,50, baos);//這里壓縮50%,把壓縮後的數據存放到baos中

}

ByteArrayInputStream isBm =newByteArrayInputStream(baos.toByteArray());

BitmapFactory.Options newOpts =newBitmapFactory.Options();

//開始讀入圖片,此時把options.inJustDecodeBounds 設回true了

newOpts.inJustDecodeBounds =true;

Bitmap bitmap = BitmapFactory.decodeStream(isBm,null, newOpts);

newOpts.inJustDecodeBounds =false;

intw = newOpts.outWidth;

inth = newOpts.outHeight;

//現在主流手機比較多是800*480解析度,所以高和寬我們設置為

floathh = 800f;//這里設置高度為800f

floatww = 480f;//這里設置寬度為480f

//縮放比。由於是固定比例縮放,只用高或者寬其中一個數據進行計算即可

intbe =1;//be=1表示不縮放

if(w > h && w > ww) {//如果寬度大的話根據寬度固定大小縮放

be = (int) (newOpts.outWidth / ww);

}elseif(w < h && h > hh) {//如果高度高的話根據寬度固定大小縮放

be = (int) (newOpts.outHeight / hh);

}

if(be <=0)

be =1;

newOpts.inSampleSize = be;//設置縮放比例

//重新讀入圖片,注意此時已經把options.inJustDecodeBounds 設回false了

isBm =newByteArrayInputStream(baos.toByteArray());

bitmap = BitmapFactory.decodeStream(isBm,null, newOpts);

returncompressImage(bitmap);//壓縮好比例大小後再進行質量壓縮

}

3. android開發,上傳圖片時,怎樣自動壓縮成800x600的尺寸求指教~~

protected Bitmap scaleImg(Bitmap bm, int newWidth, int newHeight) {
// 圖森螞纖片源
// Bitmap bm = BitmapFactory.decodeStream(getResources()
// .openRawResource(id));
// 獲得圖片的寬高
int width = bm.getWidth();
int height = bm.getHeight();
// 設置想要的物賀大小
int newWidth1 = newWidth;
int newHeight1 = newHeight;
// 計算縮放比此仿例
float scaleWidth = ((float) newWidth1) / width;
float scaleHeight = ((float) newHeight1) / height;
// 取得想要縮放的matrix參數
Matrix matrix = new Matrix();
matrix.postScale(scaleWidth, scaleHeight);
// 得到新的圖片
Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix,
true);
return newbm;

}
你可以試下,如果不行,建議你還是先壓縮在上傳。

4. android 圖片上傳有什麼方式可以壓縮

非常簡單,楓齡EYE網 導航欄右邊有在散李輪線壓縮。而且是無損解壓,沖信不改變質量只變小。
點擊後擾培,點upload上傳,等待一會然後提示你download下載就可以了
支持批量解壓,但是時間有點長。

5. android中壓縮圖片的幾種方法比較

圖片有三種存在形式:硬碟上時是file,網路傳輸時是stream,內存中是stream或bitmap,所謂的質量壓縮,它其實只能實現對 file的影響,你可以把一個file轉成bitmap再轉成file,或者直接將一個bitmap轉成file時,這個最終的file是被壓縮過的,但 是中間的bitmap並沒有被壓縮(或者說幾乎沒有被壓縮,我不確定),因為bigmap在內存中的大小是按像素計算的,也就是width * height,對於質量壓縮,並不會改變圖片的像素,所以就算質量被壓縮了,但是bitmap在內存的佔有率還是沒變小,但你做成file時,它確實變小 了;
而尺寸壓縮由於是減小了圖片的像素,所以它直接對bitmap產生了影響,當然最終的file也是相對的變小了;

6. Android壓縮圖片的問題

java">//可能是裁剪方法或存儲方法的原因下面的源碼希望能幫到你
/**
*裁剪圖片方法實現
*
*@paramuri
*/
publicvoidstartPhotoZoom(Uriuri){
Intentintent=newIntent("com.android.camera.action.CROP");
intent.setDataAndType(uri,"image/*");
//設置裁剪
intent.putExtra("crop","true");
//aspectXaspectY是寬高的比例
intent.putExtra("aspectX",1);
intent.putExtra("aspectY",1);
//outputXoutputY是裁剪圖片寬高
intent.putExtra("outputX",180);
intent.putExtra("outputY",180);
intent.putExtra("return-data",true);
startActivityForResult(intent,2);
}
(Stringpath,BitmapmBitmap)throwsIOException{
Filefile=newFile(path);
if(!file.exists()){
file.createNewFile();
}
FileOutputStreamfOut=null;
猛信首try{
坦扮fOut=newFileOutputStream(file);
}catch(FileNotFoundExceptione){
e.printStackTrace();
}
mBitmap.compress(Bitmap.CompressFormat.JPEG,100,fOut);
try{
fOut.flush();
}catch(IOExceptione){
e.printStackTrace();
returnfalse;
}
try{
fOut.close();
}catch(IOExceptione){
枝數e.printStackTrace();
returnfalse;
}
returntrue;
}

7. Android黑科技,圖片終極壓縮

一、支持自定義配置、不失真和批量處理

二、圖片上傳為什麼要壓縮
1、圖片伺服器空間限制,磁碟昂貴
2、網路不穩定,大文件需要斷點續傳
3、盡可能避免安卓OOM異常
4、後台約定的規則<200KB
5、需要上傳原圖的應用有醫院臨床項目、金融銀行

三、圖片壓縮流程
1、遞歸每張圖片
2、設置圖片格式 Bitmap.CompressFormat.JPG
png, jpg,webp
3、質量壓縮bitmap.compress(format,quality,baos)
由於png是無損壓縮,所以設置quality無效(不適合作為縮略圖)
采樣率壓縮BitmapFactory.Options.inSampleSize
縮小圖片解析度,減少所佔用磁碟空間和內存大小
縮放壓縮canvas.drawBitmap(bitmap, null,rectF,null)
減少圖片的像素,降低所佔用磁碟空間大小和內存大小,可用於緩存縮略圖
JNI調用JPEG庫
Android的圖片引擎使用的是閹割版的skia引擎,去掉了圖片壓縮中的哈夫曼演算法
4、像素修復
5、返回壓縮
6、完成壓縮

demo: https://github.com/ApeCold/Learn_Compress_Sample

參考:
Luban框架 https://github.com/Curzibn/Luban
缺點
1、當沒有設定壓縮路徑時,拋異常無閃退
2、源碼中,壓縮比率固定值60,無法修改
3、壓縮配置,參數不太適應真實項目需求
4、不能指定壓縮大小,比如100KB以內
https://github.com/zettsu/Compressor

8. android 圖片壓縮

在文件管理裡面長按圖片會出現設置面板,選中壓縮圖片即可以。

9. 說說在 Android 中如何壓縮圖片

1:ByteArrayOutputStream 壓縮圖片質量,不會改變圖片大小

privateBitmapcompressImage(Bitmapimage){

ByteArrayOutputStreambaos=newByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG,100,baos);//質量壓縮方法,這里100表示不壓縮,把壓縮後的數據存放到baos中
intoptions=100;while(baos.toByteArray().length/1024>100){//循環判斷如果壓縮後圖片是否大於100kb,大於繼續壓縮
baos.reset();//重置baos即清空baos
image.compress(Bitmap.CompressFormat.JPEG,options,baos);//這里壓縮options%,把壓縮後的數據存放到baos中
options-=10;//每次都減少10}
ByteArrayInputStreamisBm=newByteArrayInputStream(baos.toByteArray());//把壓縮後的數據baos存放到ByteArrayInputStream中
Bitmapbitmap=BitmapFactory.decodeStream(isBm,null,null);//把ByteArrayInputStream數據生成圖片
returnbitmap;
}

2:就是改變圖片大小

privateBitmapgetimage(StringsrcPath){
BitmapFactory.OptionsnewOpts=newBitmapFactory.Options();//開始讀入圖片,此時把options.inJustDecodeBounds設回true了
newOpts.inJustDecodeBounds=true;
Bitmapbitmap=BitmapFactory.decodeFile(srcPath,newOpts);//此時返回bm為空
newOpts.inJustDecodeBounds=false;intw=newOpts.outWidth;inth=newOpts.outHeight;//現在主流手機比較多是800*480解析度,所以高和寬我們設置為
floathh=800f;//這里設置高度為800f
floatww=480f;//這里設置寬度為480f//縮放比。由於是固定比例縮放,只用高或者寬其中一個數據進行計算即可
intbe=1;//be=1表示不縮放
if(w>h&&w>ww){//如果寬度大的話根據寬度固定大小縮放
be=(int)(newOpts.outWidth/ww);
}elseif(w<h&&h>hh){//如果高度高的話根據寬度固定大小縮放
be=(int)(newOpts.outHeight/hh);
}if(be<=0)
be=1;
newOpts.inSampleSize=be;//設置縮放比例//重新讀入圖片,注意此時已經把options.inJustDecodeBounds設回false了
bitmap=BitmapFactory.decodeFile(srcPath,newOpts);returncompressImage(bitmap);//壓縮好比例大小後再進行質量壓縮}
熱點內容
隨機啟動腳本 發布:2025-07-05 16:10:30 瀏覽:528
微博資料庫設計 發布:2025-07-05 15:30:55 瀏覽:25
linux485 發布:2025-07-05 14:38:28 瀏覽:305
php用的軟體 發布:2025-07-05 14:06:22 瀏覽:756
沒有許可權訪問計算機 發布:2025-07-05 13:29:11 瀏覽:432
javaweb開發教程視頻教程 發布:2025-07-05 13:24:41 瀏覽:707
康師傅控流腳本破解 發布:2025-07-05 13:17:27 瀏覽:243
java的開發流程 發布:2025-07-05 12:45:11 瀏覽:686
怎麼看內存卡配置 發布:2025-07-05 12:29:19 瀏覽:285
訪問學者英文個人簡歷 發布:2025-07-05 12:29:17 瀏覽:835