我想上傳圖片大小相同選擇從Gallary.i.Uploaded到服務器..But圖像上傳到服務器上像壓縮....像這張圖片下方 如何上傳圖片到服務器在android
我想在相同大小...或如何設置裁剪意圖在這了Methode上傳圖片
這裏是我的代碼來調整圖像
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case 123987:
if (resultCode == RESULT_OK) {
try {
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
Log.d("selectedImagePath", "selectedImagePath = " + selectedImagePath);
ParseImage _ParseImage = new ParseImage();
_ParseImage.activityContext = this;
_ParseImage.execute(selectedImagePath);
} catch (Exception e) {
// TODO: handle exception
}
}
break;
case 1234:
if (resultCode == RESULT_OK) {
try {
final_bitmap = null;
String realPath = Environment.getExternalStorageDirectory() + "/" + fileName;
selectedImagePath = realPath;
new BitmapFactory();
Bitmap btmp = setBitmap(selectedImagePath);
/*String path = selectedImagePath.toLowerCase();
if (path.contains("dcim") && path.contains("camera")) {
btmp = RotateBitmap(btmp, 90);
}*/
int o_width = btmp.getWidth();
int o_height = btmp.getHeight();
// w=1840, h=3264
float large_image_ratio = 300.00f;
//Log.d("orignal_image", "orignal_image = " + o_width + " , " + o_height);
if (o_width > o_height) {
if (o_width >= large_image_ratio) {
float temp_w = (float)o_width/(float)large_image_ratio;
//int temp_h = o_height/large_image_ratio;
//Log.d("temp_w", "temp_w = " + temp_w);
int scales_width = (int) large_image_ratio;
int scales_height = (int) Math.round(o_height/temp_w);
//Log.d("scale_image-if", "scale_image-if = " + scales_width + " , " + scales_height);
final_bitmap = Bitmap.createScaledBitmap(btmp, scales_width, scales_height, true);
} else {
final_bitmap = btmp;
}
} else {
if (o_height >= large_image_ratio) {
//int temp_w = o_width/large_image_ratio;
float temp_h = (float)o_height/(float)large_image_ratio;
//Log.d("temp_h", "temp_h = " + temp_h);
int scales_width = (int) Math.round(o_width/temp_h);
int scales_height = (int) large_image_ratio;
//Log.d("scale_image-else", "scale_image-else = " + scales_width + " , " + scales_height);
final_bitmap = Bitmap.createScaledBitmap(btmp, scales_width, scales_height, true);
} else {
final_bitmap = btmp;
}
}
img.setImageBitmap(final_bitmap);
Image_into_Byte(final_bitmap);
img.setVisibility(View.VISIBLE);
btn_upload.setEnabled(true);
} catch (Exception e) {
// TODO: handle exception
}
}
break;
}
}
public class ParseImage extends AsyncTask<String, Void, String> {
private ProgressDialog dialog;
protected Context activityContext;
@Override
protected void onPreExecute() {
try {
final_bitmap = null;
this.dialog = new ProgressDialog(activityContext, AlertDialog.THEME_HOLO_LIGHT);
//this.dialog.setTitle(title);
this.dialog.setMessage("Loading Image");
this.dialog.setCanceledOnTouchOutside(false);
this.dialog.show();
} catch (Exception e) {
// TODO: handle exception
}
}
protected String doInBackground(String... image_path) {
try {
new BitmapFactory();
Bitmap btmp = setBitmap(selectedImagePath);
/*String path = selectedImagePath.toLowerCase();
if (path.contains("dcim") && path.contains("camera")) {
btmp = RotateBitmap(btmp, 90);
}*/
int o_width = btmp.getWidth();
int o_height = btmp.getHeight();
// w=1840, h=3264
float large_image_ratio = 300.00f;
//Log.d("orignal_image", "orignal_image = " + o_width + " , " + o_height);
if (o_width > o_height) {
if (o_width >= large_image_ratio) {
float temp_w = (float)o_width/(float)large_image_ratio;
//int temp_h = o_height/large_image_ratio;
//Log.d("temp_w", "temp_w = " + temp_w);
int scales_width = (int) large_image_ratio;
int scales_height = (int) Math.round(o_height/temp_w);
//Log.d("scale_image-if", "scale_image-if = " + scales_width + " , " + scales_height);
final_bitmap = Bitmap.createScaledBitmap(btmp, scales_width, scales_height, true);
} else {
final_bitmap = btmp;
}
} else {
if (o_height >= large_image_ratio) {
//float temp_w = o_width/large_image_ratio;
float temp_h = (float)o_height/(float)large_image_ratio;
//Log.d("temp_h", "temp_h = " + temp_h);
int scales_width = (int) Math.round(o_width/temp_h);
int scales_height = (int) Math.round(o_width/temp_h);
//Log.d("scale_image-else", "scale_image-else = " + scales_width + " , " + scales_height);
final_bitmap = Bitmap.createScaledBitmap(btmp, scales_width, scales_height, true);
} else {
final_bitmap = btmp;
}
}
} catch (Exception e) {
// TODO: handle exception
}
return "complete";
}
@Override
protected void onPostExecute(String result) {
try {
this.dialog.cancel();
SaveBitmap(final_bitmap);
// final_bitmap
img.setImageBitmap(final_bitmap);
Image_into_Byte(final_bitmap);
btn_upload.setEnabled(true);
} catch (Exception e) {
// TODO: handle exception
}
}
}
然後不要弄亂位圖和縮放和調整大小。它只是一個你想要上傳的文件。上傳文件的示例,您可以在此網站上找到許多文件。 – greenapps 2014-09-11 07:00:33