2014-06-16 34 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) 
以其他方式