如何上传图片到服务器在android
问题描述:
我想上传图片大小相同选择从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
}
}
}
答
// crop intent function
public void cropCapturedImage(Uri picUri){
//call the standard crop action intent
Intent cropIntent = new Intent("com.android.camera.action.CROP");
//indicate image type and Uri of image
cropIntent.setDataAndType(picUri, "image/*");
//set crop properties
cropIntent.putExtra("crop", "true");
//indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
//indicate output X and Y
cropIntent.putExtra("outputX", 256);
cropIntent.putExtra("outputY", 256);
//retrieve data on return
cropIntent.putExtra("return-data", true);
//start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, 2);
}
get uri from gallery selected image
private String getRealPathFromURI(Uri contentURI) {
String result;
Cursor cursor = getContentResolver().query(contentURI, null, null, null, null);
if (cursor == null) { // Source is Dropbox or other similar local file path
result = contentURI.getPath();
} else {
cursor.moveToFirst();
int idx = cursor.getColumnIndex(MediaStore.Images.ImageColumns.DATA);
result = cursor.getString(idx);
cursor.close();
}
return result;
}
use this function when get image from gallery and camera :-
// camera on activityresult
cropCapturedImage(Uri.fromFile(file));
// gallery
cropCapturedImage(Uri.fromFile(getRealPathFromURI(data.getData())));
答
您应该制作Base64
编码的字符串Bitmap
并通过NameValuePair
与url
将此图像字符串发送到服务器。使用AsyncTask<...>
上传图像例如像:
private class ImgUploadingTask extends AsyncTask<String, Void, String>
{
//do your stuff onPreExecute()
@Override
protected String doInBackground(String... params)
{
try
{
String imageDataString = null;
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(server_url);
List<NameValuePair> list = new ArrayList<NameValuePair>();
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
imageDataString = Base64.encodeToString(data, Base64.DEFAULT);
list.add(new BasicNameValuePair("image", imageDataString));
post.setEntity(new UrlEncodedFormEntity(list));
HttpResponse resp = client.execute(post);
HttpEntity entity = resp.getEntity();
String response = EntityUtils.toString(entity);
Log.d("Response ", response);
}
catch(Exception e){
e.printStackTrace();
}
return null;
}
}
//do your stuff onPostExecute()
这一点,你必须设置你的服务器端代码后,接受这个Base64 encoded
字符串和decode
它在服务器端得到实时图像。
+0
Base64编码图像不是推荐的方式,因为它将传输量增加了30%。更好地发送文件'原样'。 – greenapps 2014-09-11 07:02:15
然后不要弄乱位图和缩放和调整大小。它只是一个你想要上传的文件。上传文件的示例,您可以在此网站上找到许多文件。 – greenapps 2014-09-11 07:00:33