gpt4 book ai didi

java - 使用base64在android中上传图片和发送参数

转载 作者:搜寻专家 更新时间:2023-11-01 08:09:35 26 4
gpt4 key购买 nike

我正在用安卓上传。为此,我正在关注此 tutorial .我当前的代码在我的 logcat 中给出了这个错误

06-23 10:10:22.990: D/dalvikvm(25853): GC_EXTERNAL_ALLOC freed 48K, 50% free 2723K/5379K, external 0K/0K, paused 33ms
06-23 10:10:23.030: E/log_tag(25853): Error in http connection java.net.UnknownHostException: www.example.info
06-23 10:10:23.115: D/CLIPBOARD(25853): Hide Clipboard dialog at Starting input: finished by someone else... !

我的代码是这样的

public class UploadFileActivity extends Activity {
/** Called when the activity is first created. */
Button bUpload;
EditText etParam;

InputStream is;

@Override

public void onCreate(Bundle icicle) {

super.onCreate(icicle);
setContentView(R.layout.main);

Bitmap bitmapOrg = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(Bitmap.CompressFormat.JPEG, 90, bao);

byte [] ba = bao.toByteArray();
String ba1=Base64.encodeToString(ba, 0);

ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",ba1));

try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.example.info/androidfileupload/index.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
} catch(Exception e) {
Log.e("log_tag", "Error in http connection "+e.toString());
}

}
}

这是我在服务器端使用的

<?php
$base=$_REQUEST['image'];

echo $base;

// base64 encoded utf-8 string

$binary=base64_decode($base);

// binary, utf-8 bytes

header('Content-Type: bitmap; charset=utf-8');

// print($binary);

//$theFile = base64_decode($image_data);

$file = fopen('test.jpg', 'wb');

fwrite($file, $binary);

fclose($file);

echo '<img src=test.jpg>';

?>

最佳答案

UnknownHostException 与您的 url 域相关或没有 Internet/网络连接,

因此请确保您已在 AndroidManifest 文件中添加互联网权限。

<uses-permission android:name="android.permission.INTERNET" />

希望对您有所帮助

关于java - 使用base64在android中上传图片和发送参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11167686/

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