Android 无法使用毕加索调整图像大小
嗨,我无法使用毕加索调整图像大小。我想调整图像大小并将其发送到服务器。我已将图像保存到我的内部目录中,我正在使用mCurrentPhotoPath访问它。该文件已上载到服务器,但未调整大小。感谢您的帮助Android 无法使用毕加索调整图像大小,android,Android,嗨,我无法使用毕加索调整图像大小。我想调整图像大小并将其发送到服务器。我已将图像保存到我的内部目录中,我正在使用mCurrentPhotoPath访问它。该文件已上载到服务器,但未调整大小。感谢您的帮助 private File createImageFile() throws IOException { OutputStream outStream = null; // Create an image file name String timeStamp = new
private File createImageFile() throws IOException {
OutputStream outStream = null;
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "JPEG_" + timeStamp + "_";
File storageDir = getExternalFilesDir(Environment.DIRECTORY_PICTURES);
File image = File.createTempFile(
imageFileName, /* prefix */
".jpg", /* suffix */
storageDir /* directory */
);
mCurrentPhotoPath = image.getAbsolutePath();
try {
outStream = new FileOutputStream(mCurrentPhotoPath);
Bitmap bitmap = Picasso.get()
.load(mCurrentPhotoPath)
.resize(80, 80).fit()
.centerInside().get();
bitmap = getResizedBitmap(bitmap,20);
bitmap.compress(Bitmap.CompressFormat.PNG, 0, outStream);
outStream.flush();
outStream.close();
// Save a file: path for use with ACTION_VIEW intents
Log.i("file path before", mCurrentPhotoPath);
} catch (Exception e) {
Log.i("exception", e.toString());
}
return image;
}
public Bitmap getResizedBitmap(Bitmap image, int maxSize) {
int width = image.getWidth();
int height = image.getHeight();
float bitmapRatio = (float) width / (float) height;
if (bitmapRatio > 1) {
width = maxSize;
height = (int) (width / bitmapRatio);
} else {
height = maxSize;
width = (int) (height * bitmapRatio);
}
return Bitmap.createScaledBitmap(image, width, height, true);
}
文件大小为3-4MB。我想要最大600KB的文件。我希望您应该将图像压缩为一个基本的64位字符串,然后发送到服务器,如下代码,您必须在这里使用位图
Bitmap bitmap=/*intialize bitmap by your image's bitmap*/;
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 70, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
String encodedImage1 = Base64.encodeToString(byteArray, Base64.DEFAULT);
// encodedImage1 for passing to server
谢谢我正在使用multipart request上传文件,因此我不想将图像压缩到base64 multipart不推荐使用,请停止使用它。我正在使用“net.gotev:uploadservice:3.1”。这是不推荐的吗?你可能想看看这个答案。这与您的问题不同,但这段代码包含调整大小、压缩照片,甚至显示了使用改装将照片上载到服务器的示例代码