在Android中转换图像

时间:2014-06-16 09:40:26

标签: android image bitmap

我在这里面临一些问题。我有一个套接字服务器,我的应用程序上传了图像。现在,如果我说我有100万客户,每个1mb用于图像,我应该留下1TB只是个人资料图像,这太多了。我正在寻找一种方法将所有图像文件转换为最大100 kb大小,这样的事情可能吗?如果是这样我应该搜索什么?

我也从磁盘中选择图像文件,如下所示:

  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

相关的活动中

2 个答案:

答案 0 :(得分: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是压缩文件格式

答案 1 :(得分:0)

有两种缩放位图的方法:

给定尺寸:

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

采样图片,使用:

BitmapFactory.decodeFile(file, options)