gpt4 book ai didi

android - 尝试在 php 服务器中上传图片,但它可以发布。在安卓中

转载 作者:行者123 更新时间:2023-11-29 16:10:17 24 4
gpt4 key购买 nike

我尝试通过图库将图像上传到服务器中,其中包含名称 DOB id 等其他条目,除了 php 服务器中的图像外,所有条目都已上传,我不明白我的错误在哪里请帮忙。建议任何其他代码请告诉我。这是我的代码

          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);

最佳答案

下面的代码会先用分辨率压缩图片,然后设置MultiPart实体并通过POST方法通过网络发送数据。

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);

编辑

哦.. 对不起,我忘了通知。是的,有一个名为 httpmime-4.2.2httpclient.jar 的 jar。您可以从Apache Site下载。 .解压包,得到lib目录。

关于android - 尝试在 php 服务器中上传图片,但它可以发布。在安卓中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14027882/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com