我正在使用这种方法:
public Bitmap decodeUri(Uri selectedImage) throws FileNotFoundException {
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o);
final int REQUIRED_SIZE = 800;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 1;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE || height_tmp / 2 < REQUIRED_SIZE) {
break;
}
width_tmp /= 2;
height_tmp /= 2;
scale *= 2;
}
BitmapFactory.Options o2 = new BitmapFactory.Options();
o2.inSampleSize = scale;
return BitmapFactory.decodeStream(getContentResolver().openInputStream(selectedImage), null, o2);
}
从mi相机解码URI,然后通过POST标题中的HttpURLConnection
对象发送到我的网络服务器。但我得到了原始图像:
我得到了裁剪图像:
我不知道为什么。也许我的decodeURI
方法错了?有人知道将我的图像压缩到我的网络服务器的其他最佳方法吗?
我现在使用的是decodeUri
方法的下一个方法:
mybitMap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);
我有同样的问题。当我尝试用Photoshop在我的网络服务器中打开图像时,我看到一则广告,上面写着“这个文件可能已损坏(文件可能被截断或不完整)。你想继续吗?”并且,继续之后,我在图像的底部看到一个黑色空间......
有人知道为什么吗?我的代码:
Bitmap mybitMap = null;
try {
mybitMap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImageUri);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
imagen.compress(Bitmap.CompressFormat.JPEG, 100, baos);
我在POST标题中发送此信息:
Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);