我需要知道加载位图并通过http作为字符串发送它的最佳方法,因为我想将它存储在数据库中。那你能帮我解决一下吗?
提前致谢,问候
我有一些东西:
public byte[] ConvertBitmaptoBits(Bitmap src)
{
try{
ByteArrayOutputStream os=new ByteArrayOutputStream();
src.compress( android.graphics.Bitmap.CompressFormat.PNG, 100, (OutputStream)os );
src.compress(Bitmap.CompressFormat.PNG, 100, os); //bm is the bitmap object
byte[] b = os.toByteArray();
return b;
}catch(Throwable e)
{
//Toast.makeText(this, "Error en ConvierteBitmapAString: " + e.getMessage(), 30);
Log.v("ConvierteBitmapACadena", "Error al Convertir la imagen a Cadena: " + e.getMessage());
return null;
}
}
在我的SEND方法中,我有这样的事情:
public void Send() //throws Exception
{
try
{
InputStreamBody isb=null;
StringBody sImageString=null;
Resources r = this.getResources();
Bitmap bmp = BitmapFactory.decodeResource(r, R.drawable.icon);
byte[] objImageBits = ConvertBitmaptoBits(bmp);
if(objImageBits !=null ){
isb = new InputStreamBody(new ByteArrayInputStream(objImageBits), "uploadedFile");
}
HttpClient httpClient = new DefaultHttpClient();
HttpPost postRequest = new HttpPost(strPath);
SimpleDateFormat sdfDateTime = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
String strCurDate = sdfDateTime.format(new Date(System.currentTimeMillis()));
StringBody sCurDate = new StringBody(strCurDate);
MultipartEntity multipartContent = new MultipartEntity();
if(objImageBits !=null)
{
multipartContent.addPart("uploadedFile", isb);
}
multipartContent.addPart("fechaequipo", sCurDate);
postRequest.setEntity(multipartContent);
HttpResponse response = httpClient.execute(postRequest);
response.getEntity().getContent().close();
}
catch (Throwable e)
{
Log.v("executeMultipartPost", "Error in executeMultipartPost: " + e.getMessage());
}
}
但似乎我没有收到uploadedFile。这是我的PHP脚本:
$file2 = (isset($_POST['uploadedFile'])) ? ( $_POST['uploadedFile'] ):('');
$fechaequipo = (isset($_POST['fechaequipo']) ) ? ( $_POST['fechaequipo'] ):('');
$fp = null;
$log_file = 'log.txt';
if (!$fp)
$fp = fopen($log_file, 'a') or exit("No se puede abrir: $log_file!");
fwrite($fp, "<INI LOG>" . date("d/m/Y") ."\n\r");
fwrite($fp, "Date : ". $fechaequipo . "\n\r");
fwrite($fp, "File2 : " . $file2 . "\n\r");
fwrite($fp, "<END LOG>" . date("d/m/Y") ."\n\r");
fclose($fp);
&GT;
我做错了吗?在此先感谢!!!
答案 0 :(得分:1)
类似的问题(在某种意义上)是here。
您将图像转换为base64,然后发送到您喜欢的服务器。然后,服务器可以将字符串解码为图像。 (使用PHP,这将是base64-decode
)。
请记住,base64编码会增加about 33%传输的数据的大小。