【发布时间】:2019-05-27 03:59:54
【问题描述】:
当用户从图库中选择图像时,我将其转换为 base64 字符串。然后我使用 API。图片大小必须小于 2MB,所以如果图库中的图片大于 2MB,我需要减小它的大小。 我该怎么做?
这是我的代码:
// I pass the URI of the image to the method
@Override
protected Integer doInBackground(Uri... uris) {
InputStream inputStream = null;
try {
inputStream = getContentResolver().openInputStream(uris[0]);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
byte[] buffer = new byte[8192];
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
while ((bytesRead = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
} catch (IOException e) {
e.printStackTrace();
}
bytes = output.toByteArray();
String encodedImage = Base64.encodeToString(bytes, Base64.NO_WRAP);
【问题讨论】: