这是唯一可能的解决方案据我所知,以减少大小..
public void onActivityResult(int requestcode, int resultcode, Intent data) {
if (resultcode == RESULT_OK) {
if (requestcode == SELECT_PICTURE) {
Uri uri = data.getData();
String imagePath = getPath(uri);
int SCALE = 2;
try{
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = SCALE;
Bitmap bitmap= BitmapFactory.decodeFile(imagePath, o2);
OutputStream os = new FileOutputStream(imagePath);
bitmap.compress(Bitmap.CompressFormat.PNG, 85, os);
os.flush();
os.close();
File file = new File(imagePath);
Log.i("Bitmap", "Height Width "+bitmap.getHeight()+" "+bitmap.getWidth());
Log.i("File","Size in bytes "+file.length());
while(file.length()>100*1024)
{
SCALE +=2;
o2.inSampleSize = SCALE;
bitmap= BitmapFactory.decodeFile(imagePath, o2);
os = new FileOutputStream(imagePath);
bitmap.compress(Bitmap.CompressFormat.PNG, 85, os);
os.flush();
os.close();
file = new File(imagePath);
Log.i("Bitmap", "Height Width "+bitmap.getHeight()+" "+bitmap.getWidth());
Log.i("File","Size in bytes "+file.length());
}
bitmap = BitmapFactory.decodeFile(imagePath, o2);
}
catch (Exception e) {
e.printStackTrace();
}
txtimagepath.setText(imagePath);
}
}
}
protected String getPath(Uri uri) {
String[] projection = { MediaStore.Images.Media.DATA };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int columnIndex = cursor
.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(columnIndex);
}
当您更改宽度和高度
的照片,那么显然字节的大小也会改变。你试过了吗? –
确定我会尝试,但我想保持照片的高度和宽度... –
我不认为这是可能的高度和宽度不变,改变大小。虽然可能,但图像的质量也将分别降低。 –