尝试在php服务器中上载图像,但它可能会发布。在android中
我试图通过图库上传服务器上的图片和其他条目,如名称DOB id等,所有条目都是上传的,除了php服务器上的图片,我不知道哪里是我的错误,请帮助。建议任何其他代码请告诉我。 这是我的密码尝试在php服务器中上载图像,但它可能会发布。在android中,android,image-uploading,Android,Image Uploading,我试图通过图库上传服务器上的图片和其他条目,如名称DOB id等,所有条目都是上传的,除了php服务器上的图片,我不知道哪里是我的错误,请帮助。建议任何其他代码请告诉我。 这是我的密码 Bitmap bitmapOrg = BitmapFactory.decodeFile(imageselectedPath1); ByteArrayOutputStream bao = new ByteArrayOutputStream(); // Resi
Bitmap bitmapOrg = BitmapFactory.decodeFile(imageselectedPath1);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
// Resize the image
double width = bitmapOrg.getWidth();
double height = bitmapOrg.getHeight();
double ratio = 400 / width;
int newheight = (int) (ratio * height);
System.out.println("———-width" + width);
System.out.println("———-height" + height);
Log.d("width ", "width " + width);
Log.d("height ", "height " + height);
bitmapOrg = Bitmap.createScaledBitmap(bitmapOrg, 400,
newheight, true);
// Here you can define .PNG as well
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 99, bao);
byte[] ba = bao.toByteArray();
String ba1 = Base64.encodeBytes(ba);
Log.d("uploading ", "uploading " + ba1);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("user_id", mSignMessg));
nameValuePairs.add(new BasicNameValuePair("name", mname.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("dob", mbirthday.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("bio", mbio.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("sex", mSexValue));
nameValuePairs.add(new BasicNameValuePair("profile_status", "0"));
nameValuePairs.add(new BasicNameValuePair("location", mlocation.getText().toString()));
nameValuePairs.add(new BasicNameValuePair("image", ba1));
Log.d("userfile", "userfile " + ba1);
// nameValuePairs.add(new BasicNameValuePair("image”, ba1));
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(
"http://iapptechnologies.com/snapic/profileXml.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
Log.d("nameValuePairs", "nameValuePairs " + nameValuePairs);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
// print responce
outPut = EntityUtils.toString(entity);
Log.i("GET RESPONSE—-", outPut);
Log.d("GET RESPONSE—-", outPut);
// is = entity.getContent();
Log.e("log_tag ******", "good connection");
System.out.println("gudconection");
Log.d("god connection ", "gud connection ");
bitmapOrg.recycle();
} catch (Exception e) {
Log.e("logCatch block***",
"Error in http connection " + e.toString());
Log.d("log_catch block ******", "Error in http connection "
+ e.toString());
}
Log.d("image_name","image_name "+image_name);
位图位图位图org=BitmapFactory.decodeFile(imageselectedPath1);
ByteArrayOutputStream bao=新建ByteArrayOutputStream();
//调整图像大小
double-width=bitmapOrg.getWidth();
double height=bitmapOrg.getHeight();
双倍比=400/宽;
int newheight=(int)(比率*高度);
System.out.println(“----宽度”+宽度);
System.out.println(“----高度”+高度);
对数d(“宽度”、“宽度”+宽度);
对数d(“高度”、“高度”+高度);
bitmapOrg=Bitmap.createScaledBitmap(bitmapOrg,400,
新高度,正确);
//在这里您还可以定义.PNG
压缩(Bitmap.CompressFormat.JPEG,99,bao);
字节[]ba=bao.toByteArray();
字符串ba1=Base64.encodeBytes(ba);
日志d(“上传”、“上传”+ba1);
ArrayList nameValuePairs=新的ArrayList();
添加(新的BasicNameValuePair(“用户id”,mSignMessg));
添加(新的BasicNameValuePair(“name”,mname.getText().toString());
添加(新的BasicNameValuePair(“dob”,mbirthday.getText().toString());
添加(新的BasicNameValuePair(“bio”,mbio.getText().toString());
添加(新的BasicNameValuePair(“sex”,mSexValue));
添加(新的BasicNameValuePair(“profile_status”,“0”);
添加(新的BasicNameValuePair(“location”,mlocation.getText().toString());
添加(新的BasicNameValuePair(“图像”,ba1));
Log.d(“userfile”、“userfile”+ba1);
//添加(新的BasicNameValuePair(“图像”,ba1));
试一试{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(
"http://iapptechnologies.com/snapic/profileXml.php");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
Log.d(“nameValuePairs”、“nameValuePairs”+nameValuePairs);
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
//打印响应
输出=EntityUtils.toString(实体);
i(“获取响应--”,输出);
d(“获取响应--”,输出);
//is=entity.getContent();
Log.e(“Log_tag*******”,“良好连接”);
System.out.println(“GUDConnection”);
Log.d(“上帝连接”、“gud连接”);
bitmapporg.recycle();
}捕获(例外e){
Log.e(“logCatch块***”,
“http连接错误”+e.toString());
Log.d(“Log_catch block*******,“http连接错误”
+e.toString());
}
Log.d(“图像名称”、“图像名称”+图像名称);
以下代码将首先以分辨率压缩图像,然后设置MultiPart
实体,并通过POST
方法通过web发送数据
BitmapFactory.Options options = new BitmapFactory.Options();
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = true;
// image path `String` where your image is located
BitmapFactory.decodeFile(imagePath, options);
int bitmapWidth = 400;
int bitmapHeight = 250;
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > bitmapHeight || width > bitmapWidth) {
if (width > height) {
inSampleSize = Math
.round((float) height / (float) bitmapHeight);
} else {
inSampleSize = Math.round((float) width / (float) bitmapWidth);
}
}
options.inJustDecodeBounds = false;
options.inSampleSize = inSampleSize;
// you can change the format of you image compressed for what do you
// want;
// now it is set up to 640 x 480;
Bitmap bmpScale = BitmapFactory.decodeFile(imagePath, options);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
// CompressFormat set up to JPG, you can change to PNG or whatever you
// want;
bmpScale.compress(CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("avatar", new ByteArrayBody(data,"pic.jpg"
));
//add your other name value pairs in entity.
for (int i = 0; i < nameValuePairs.size(); i++) {
entity.addPart(nameValuePairs.get(i).getName(), new StringBody(
nameValuePairs.get(i).getValue()));
}
httppost.setEntity(entity);
HttpResponse response = httpClient.execute(httppost);
BitmapFactory.Options=new-BitmapFactory.Options();
//使用inSampleSize集合解码位图
options.inJustDecodeBounds=true;
//图像路径'String',图像所在的位置
解码文件(imagePath,选项);
int bitmapWidth=400;
int bitmapHeight=250;
最终内部高度=options.outHeight;
最终整数宽度=options.outWidth;
int inSampleSize=1;
如果(高度>位图高度| |宽度>位图宽度){
如果(宽度>高度){
inSampleSize=数学
.圆形((浮动)高度/(浮动)位图高度);
}否则{
inSampleSize=数学圆((浮点)宽度/(浮点)位图宽度);
}
}
options.inJustDecodeBounds=false;
options.inSampleSize=inSampleSize;
//您可以更改压缩图像的格式,以便执行以下操作:
//缺乏;
//现在设置为640x480;
位图bmpScale=BitmapFactory.decodeFile(图像路径,选项);
ByteArrayOutputStream bos=新建ByteArrayOutputStream();
//压缩格式设置为JPG,您可以更改为PNG或任何您需要的格式
//缺乏;
压缩(CompressFormat.JPEG,100,bos);
字节[]数据=bos.toByteArray();
多方实体=新多方实体(
HttpMultipartMode.BROWSER_兼容);
entity.addPart(“阿凡达”,新的ByteArrayBody(数据,“pic.jpg”)
));
//在实体中添加其他名称-值对。
对于(int i=0;i
编辑
哦……对不起,我忘了通知你了。
是的,有一个名为httpime-4.2.2
和httpclient.jar
的jar。您可以从下载它。
只需解压缩包,就可以得到
lib
目录,下面的代码将首先以分辨率压缩图像,然后设置MultiPart
实体,并通过POST
方法通过web发送数据
BitmapFactory.Options options = new BitmapFactory.Options();
// Decode bitmap with inSampleSize set
options.inJustDecodeBounds = true;
// image path `String` where your image is located
BitmapFactory.decodeFile(imagePath, options);
int bitmapWidth = 400;
int bitmapHeight = 250;
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > bitmapHeight || width > bitmapWidth) {
if (width > height) {
inSampleSize = Math
.round((float) height / (float) bitmapHeight);
} else {
inSampleSize = Math.round((float) width / (float) bitmapWidth);
}
}
options.inJustDecodeBounds = false;
options.inSampleSize = inSampleSize;
// you can change the format of you image compressed for what do you
// want;
// now it is set up to 640 x 480;
Bitmap bmpScale = BitmapFactory.decodeFile(imagePath, options);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
// CompressFormat set up to JPG, you can change to PNG or whatever you
// want;
bmpScale.compress(CompressFormat.JPEG, 100, bos);
byte[] data = bos.toByteArray();
MultipartEntity entity = new MultipartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("avatar", new ByteArrayBody(data,"pic.jpg"
));
//add your other name value pairs in entity.
for (int i = 0; i < nameValuePairs.size(); i++) {
entity.addPart(nameValuePairs.get(i).getName(), new StringBody(
nameValuePairs.get(i).getValue()));
}
httppost.setEntity(entity);
HttpResponse response = httpClient.execute(httppost);
BitmapFactory.Options=new-BitmapFactory.Options();
//使用inSampleSize集合解码位图
options.inJustDecodeBounds=true;
//图像路径'String',图像所在的位置
解码文件(imagePath,选项);
整型位图