我正在使用此代码压缩图像..和ftp上传图像。我在我的设备中获取压缩图像,但后端图像已损坏。我有相机和图库选择选项上传图像。
public String compressImage(String filePath) {
Bitmap scaledBitmap = null;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap bmp = BitmapFactory.decodeFile(filePath, options);
int actualHeight = options.outHeight;
int actualWidth = options.outWidth;
float maxHeight = 1200.0f;
float maxWidth = 600.0f;
float imgRatio = actualWidth / actualHeight;
float maxRatio = maxWidth / maxHeight;
if (actualHeight > maxHeight || actualWidth > maxWidth) {
if (imgRatio < maxRatio) {
imgRatio = maxHeight / actualHeight;
actualWidth = (int) (imgRatio * actualWidth);
actualHeight = (int) maxHeight;
} else if (imgRatio > maxRatio) {
imgRatio = maxWidth / actualWidth;
actualHeight = (int) (imgRatio * actualHeight);
actualWidth = (int) maxWidth;
} else {
actualHeight = (int) maxHeight;
actualWidth = (int) maxWidth;
}
}
ImageLoadingUtils utils = new ImageLoadingUtils(getApplicationContext());
options.inSampleSize = utils.calculateInSampleSize(options, actualWidth, actualHeight);
options.inJustDecodeBounds = false;
options.inDither = false;
options.inPurgeable = true;
options.inInputShareable = true;
options.inTempStorage = new byte[16 * 1024];
try {
bmp = BitmapFactory.decodeFile(filePath, options);
} catch (OutOfMemoryError exception) {
exception.printStackTrace();
}
try {
scaledBitmap = Bitmap.createBitmap(actualWidth, actualHeight, Bitmap.Config.ARGB_8888);
} catch (OutOfMemoryError exception) {
exception.printStackTrace();
}
float ratioX = actualWidth / (float) options.outWidth;
float ratioY = actualHeight / (float) options.outHeight;
float middleX = actualWidth / 2.0f;
float middleY = actualHeight / 2.0f;
Matrix scaleMatrix = new Matrix();
scaleMatrix.setScale(ratioX, ratioY, middleX, middleY);
Canvas canvas = new Canvas(scaledBitmap);
canvas.setMatrix(scaleMatrix);
canvas.drawBitmap(bmp, middleX - bmp.getWidth() / 2, middleY - bmp.getHeight() / 2, new Paint(Paint.FILTER_BITMAP_FLAG));
ExifInterface exif;
try {
exif = new ExifInterface(filePath);
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 0);
Log.d("EXIF", "Exif: " + orientation);
Matrix matrix = new Matrix();
if (orientation == 6) {
matrix.postRotate(90);
Log.d("EXIF", "Exif: " + orientation);
} else if (orientation == 3) {
matrix.postRotate(180);
Log.d("EXIF", "Exif: " + orientation);
} else if (orientation == 8) {
matrix.postRotate(270);
Log.d("EXIF", "Exif: " + orientation);
}
scaledBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);
} catch (IOException e) {
e.printStackTrace();
}
FileOutputStream out = null;
String filename = getFilename();
try {
out = new FileOutputStream(filename);
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return filename;
}
public String getFilename() {
File file = new File(Environment.getExternalStorageDirectory().getPath());
if (!file.exists()) {
file.mkdirs();
}
String uriSting = (file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".jpg");
return uriSting;
}
Async task============================================================
private class SellAsync extends AsyncTask<String, String, String>
{
final ProgressDialog progressDialog = new ProgressDialog(Sell_Main.this);
long totalSize = 0;
@Override
protected String doInBackground(String... urls)
{
String msg = "";
try
{
for (int i = 0; i < imageList.size(); i++)
{
String filePath = compressImage(imageList.get(i));
File f = new File(filePath);
new FileUploader().uploadFile(Sell_Main.this, f);
}
if (!FileUploader.flag) {
throw new Exception("File uploading failed");
}
ArrayList<NameValuePair> arr = new ArrayList<NameValuePair>();
arr.add(new BasicNameValuePair("brand", brand));
arr.add(new BasicNameValuePair("model", model));
arr.add(new BasicNameValuePair("variant", variant));
arr.add(new BasicNameValuePair("fuel_type", fuelspinner));
arr.add(new BasicNameValuePair("kilometres", km));
arr.add(new BasicNameValuePair("transmission", trans
arr.add(new BasicNameValuePair("make_year", makeyear));
arr.add(new BasicNameValuePair("Expected_price", price));
arr.add(new BasicNameValuePair("ownership", owner));
arr.add(new BasicNameValuePair("location", location));
arr.add(new BasicNameValuePair("cid", sPref.getString("id", "")));
String imagesList = "";
for (int i = 0; i < imageList.size(); i++) {
File f = new File(imageList.get(i));
imagesList += f.getName() + ",";
}
arr.add(new BasicNameValuePair("images", imagesList.substring(0, imagesList.length() - 1)));
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(urls[0]);
post.setEntity(new UrlEncodedFormEntity(arr));
HttpResponse response = client.execute(post);
msg = EntityUtils.toString(response.getEntity());
} catch (Exception e) {
msg = e.getMessage();
}
return msg;
}
答案 0 :(得分:0)
在这段代码中尝试out.close();
就像这样
try {
out = new FileOutputStream(filename);
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally {
out.close();
}