android图片上传
/**
* 图片上传
*/
public void uploadFile(Bitmap inBmp, String inCode, String inTaskNum, String inType)
{
// String imgs="/sdcard/welcome.jpg";
// Bitmap photo = BitmapFactory.decodeFile(imgs);
ByteArrayOutputStream myStream = new ByteArrayOutputStream();
inBmp.compress( Bitmap.CompressFormat.JPEG, 50, myStream );// (0
// -
// 100)压缩文件
byte[] myCopybyte = myStream.toByteArray();
int myLength = myCopybyte.length;
String myCPdata = Base64.encodeToString( myCopybyte, 0, myLength, Base64.DEFAULT );
NameValuePair myCode = new BasicNameValuePair( "o", inCode );
NameValuePair myImage = new BasicNameValuePair( "image", myCPdata );
NameValuePair myTaskNum = new BasicNameValuePair( "ti", inTaskNum );
NameValuePair myType = new BasicNameValuePair( "tp", "3" );// 配件上传
List<NameValuePair> myParams = new ArrayList<NameValuePair>();
myParams.add( myCode );
myParams.add( myImage );
myParams.add( myTaskNum );
myParams.add( myType );
try
{
String myURL = Constants.picurl;
String myResult = ApplyService.getJsonStringPost( myURL, myParams );
if (myResult.equals( "成功" ))
{
for ( int i = 0; i < recordCheckedPic; i++ )
if (i == recordCheckedPic - 1)
{
myHandler.sendEmptyMessage( 0 );
}
else
{
myHandler.sendEmptyMessage( 5 );
}
}
else
{
myHandler.sendEmptyMessage( 6 );
}
}
catch (Exception e)
{
myHandler.sendEmptyMessage( 6 );
e.printStackTrace();
}
finally
{
myHandler.sendEmptyMessage( 9 );
}
}
//service
public String getJsonStringPost(String inUrlStr, List<NameValuePair> inParams) throws Exception
{
String inResult = null;
Log.i( "msg", "图片上传url" + inUrlStr );
HttpPost myHttpRequest = new HttpPost( inUrlStr );
myHttpRequest.setEntity( (HttpEntity) new UrlEncodedFormEntity( inParams, HTTP.UTF_8 ) );
HttpResponse myHttpResponse = new DefaultHttpClient().execute( myHttpRequest );
if (myHttpResponse.getStatusLine().getStatusCode() == 200)
{
inResult = EntityUtils.toString( myHttpResponse.getEntity() );
}
else
{
inResult = "5";
}
return inResult;
}
补充:移动开发 , Android ,