Send a Picture or Image to php Server
I need to know the BEST way to load a bitmap and send it via http as a string, because i want to store it in a database. So could you help me with any ideas, please?
Thanks in advance, Regards
I had something this:
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;
}
}
In my SEND method i had something like this:
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)
{
multipartConte开发者_高级运维nt.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());
}
}
But it seems i am not getting the uploadedFile. This is my php script:
$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);
?>
Do I'm doing anything wrong? Thanks in advance!!!
A similar question (in a sense) is here.
You convert you image to base64, then send to a server however you like. The server can then decode the string to an image. (With PHP, that'd be base64-decode
).
Bear in mind that base64 encoding increases the size of the data transferred by about 33%.
精彩评论