Java 使用mysql和php在服务器上上传和下载经过编码的位图图像
我有一个小项目,在这个项目中,我必须在一个远程mysql数据库的表中上传并保存3个图像。之后,我必须从表中获取这些图像,并在android应用程序的ImageView中显示。上传和下载在两个独立的android应用程序中完成。 我想上传质量好但尺寸小的图像,所以我用位图压缩技术压缩图像,然后用base64将其编码为字符串,然后上传到服务器。 在服务器中,我编写了一个php文件,它接收编码字符串并将其保存到表中(使用longBlob) 我的问题: 1.如果我试图解码图像并将其保存到php中的表中,我会得到一个错误。 2.我直接保存编码的图像并使用json检索它,但是当我尝试在android代码中创建json对象(以获取图像)时,它的给定错误如JSONException:value[{“imagePhoto”:“/4AAQS…etc”和json.typemismatch,并由:java.lang.nullpointerexception引起 我真的不知道该怎么做,也不知道我的方法是对是错。基本上,我只希望上传的图像大小更小,质量更好,保存和检索质量更好 请帮忙……谢谢 代码: 对图像进行编码Java 使用mysql和php在服务器上上传和下载经过编码的位图图像,java,php,android,json,base64,Java,Php,Android,Json,Base64,我有一个小项目,在这个项目中,我必须在一个远程mysql数据库的表中上传并保存3个图像。之后,我必须从表中获取这些图像,并在android应用程序的ImageView中显示。上传和下载在两个独立的android应用程序中完成。 我想上传质量好但尺寸小的图像,所以我用位图压缩技术压缩图像,然后用base64将其编码为字符串,然后上传到服务器。 在服务器中,我编写了一个php文件,它接收编码字符串并将其保存到表中(使用longBlob) 我的问题: 1.如果我试图解码图像并将其保存到php中的表中,
public String getImage(String path){
try {
ByteArrayOutputStream stream=new ByteArrayOutputStream();
Bitmap photo = BitmapFactory.decodeFile(techFolder.getPath() + File.separator + path);
photo.compress(Bitmap.CompressFormat.JPEG, 0,stream);
byte[] byte_arr=stream.toByteArray();
// Encode Image to String
String encodedString=Base64.encodeToString(byte_arr, Base64.DEFAULT);
return encodedString;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
php文件
<?php
//i skipped the login and db selection details
$imagePhoto=$_POST["imagePhoto"];
$imageAddress=$_POST["imageAddress"];
$imageSignature=$_POST["imageSignature"];
//$imagePhotoa=base64_decode($imagePhoto);
//$imageAddressa=base64_decode($imageAddress);
//$imageSignaturea=base64_decode($imageSignature);
$query="INSERT INTO `a1597402_semapho` VALUES ('$imagePhoto', '$imageAddress', '$imageSignature')";
//executing query
$result=mysql_query($query) or die(mysql_error());
//$row = mysql_fetch_array($result);
if ($result) {
$response["success"] = 1;
$response["message"] = "Succesfully submitted";
die(json_encode($response));
} else{
$response["success"] = 0;
$response["message"] = "Failed to submit the details ";
die(json_encode($response));
}
mysql_close();
?>
您是否正在尝试上载jpg文件?如果是,请先不要将其转换为位图或其他格式。也不要编码到base64,因为这会导致传输的字节数增加30%。也不要使用json上载图像。停止在PHP中使用mysql函数,改为使用mysqli或PDO。谢谢各位……我找到了您尝试上载jpg文件的解决方案?如果是这样,那么不要先将其转换为位图或其他格式。也不要编码为base64,因为这会导致传输的字节数增加30%。也不要使用json上载图像。停止在PHP中使用mysql函数,改为使用mysqli或PDO。谢谢各位……我找到了解决方案
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public static String getJSONFromUrl(String url,Boolean isParam,List<NameValuePair> params ) {
// make HTTP request
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
//checking for parameters to send to server
if(isParam)
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedInputStream bis = new BufferedInputStream(is, 8190);
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int current = 0;
while ((current = bis.read()) != -1)
{
baf.append((byte)current);
}
byte[] imageData = baf.toByteArray();
// BufferedReader reader = new BufferedReader(new InputStreamReader(is, "iso-8859-1"), 8);
// StringBuilder sb = new StringBuilder();
// String line = null;
// while ((line = reader.readLine()) != null) {
// //Log.d("stringff",line);
// sb.append(line + "\n");
// }
is.close();
//json = sb.toString();
json=new String(imageData);
Log.d("stringff",json);
} catch (Exception e) {
}
// return JSON String
return json;
}
}
@Override
protected Void doInBackground(Void... arg0) {
json = JSONParser.getJSONFromUrl(STATE_URL,false,params);
Log.d("image", json);
//parsing json string
try {
jsonObj = new JSONObject(json);
} catch (JSONException e) {
// TODO Auto-generated catch block
Log.d("BREAK1","break1");
e.printStackTrace();
}
String image;
try {
image = jsonObj.getString("imagePhoto");
byte[] imageAsBytes = Base64.decode(image.getBytes(), 0);
Bitmap bp = BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length);
i.setImageBitmap(bp);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//byte[] decodedByte = Base64.decode(json, Base64.DEFAULT);
return null;
}