2014-06-16 33 views
0

我在這裏面臨一些問題。我有一個套接字服務器,我的應用程序上傳圖像。現在,如果我說我有1百萬客戶,每個1MB的圖像,我應該留下1太字節的配置文件圖像,這太多了。我正在尋找一種方法來將所有圖像文件轉換爲最大100 kb的大小,是可能的嗎?如果是的話,我應該搜索什麼?在Android中轉換圖像

而且也是我從磁盤中選擇圖像文件,像這樣:

File file = new File("storage/sdcard/LifeMatePrivate/ProfileImage 
    /ProfileImage,imagechange_1,"+imagenamer+".jpg") 

但是當你看到它只是prefix.jpg選擇圖像。有沒有辦法使用任何擴展名的文件?謝謝。

public void SaveImage(View v){ 
    System.out.println(path); 
    String Path = path; 
    //File file=new File("storage/sdcard/Pictures/reza2.jpg"); 

    Bitmap bitmap = BitmapFactory.decodeFile("storage/sdcard/Pictures 
/reza2.jpg"); 

    // you can change the format of you image compressed for what do you 
want; 
    //now it is set up to 640 x 960; 

    Bitmap bmpCompressed = Bitmap.createScaledBitmap(bitmap, 640, 960, true); 
    ByteArrayOutputStream bos = new ByteArrayOutputStream(); 

    // CompressFormat set up to JPG, you can change to PNG or whatever you 
want; 

    bmpCompressed.compress(CompressFormat.PNG, 100, bos); 


    // Intent returnIntent = new Intent(); 
// returnIntent.putExtra("result",Path); 
// setResult(RESULT_OK,returnIntent); 
// Log.i("Image",path); 
    finish(); 

} 

這個代碼是內部的活動與StartActivityForResult

+0

怎麼能我一個1600x1200的圖像轉換爲635x479? – user3694470

+0

你可以使用'Bitmap.createScaledBitmap()'方法 –

+0

'bmpCompressed'是壓縮格式的新文件..現在你必須在你上傳文件時通過它.. –

回答

0

試試這個壓縮您的圖片盯着..

Bitmap bitmap = BitmapFactory.decodeFile(imagefilepath); 

// you can change the format of you image compressed for what do you want; 
//now it is set up to 640 x 960; 

Bitmap bmpCompressed = Bitmap.createScaledBitmap(bitmap, 640, 960, true); 
ByteArrayOutputStream bos = new ByteArrayOutputStream(); 

// CompressFormat set up to JPG, you can change to PNG or whatever you want; 

bmpCompressed.compress(CompressFormat.JPEG, 100, bos); 

現在bmpCompressed是一個壓縮文件格式

+0

謝謝,即時通訊工作在此 – user3694470

+0

確實有效嗎? –

+0

由於某些原因,這不會改變它的大小,甚至有點新的文件地址是一樣的權利? – user3694470

0

有2種方式縮放位圖:

隨着給定大小:

Bitmap yourBitmap; 
Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, newWidth, newHeight, true); 

採樣圖像使用:

BitmapFactory.decodeFile(file, options) 
以其他方式