您好我无法使用Picasso调整图像大小。我想调整图像大小并将其发送到服务器。我已将图像保存到我的内部目录,并使用mCurrentPhotoPath访问它。该文件上传到服务器但没有调整大小。任何帮助表示赞赏。
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 - 4 MB。我想要最大600KB的文件。
答案 0 :(得分:0)
我希望您将图像压缩为Base 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
由于