我正在使用HttpClient从我的Android设备向一个简单的HTTP服务器发出请求,该服务器包含一个用于检索图片的PHP脚本。图片在blob数据中可用。这意味着如果我在服务器端执行这个小PHP代码:
file_put_contents("myPicture.png", $blob_data);
我使用名为myPicture.png的文件将图片保存在服务器中。现在我想得到这个$ blob_data(我的图片)将它保存在我的Android设备中,而不是HTTP服务器中。有人可以给我一个提示,从PHP脚本返回blob数据,并在Android设备中获取图片,以便在本地存储吗?
这是我的HTTPClient:
@Click(R.id.btn_login)
@Background
public void LoginTrigger(){
String nUsername = username.getText().toString().trim();
String nPassword = password.getText().toString().trim();
if (nUsername.matches("") || nPassword.matches("")){
displayToast("Please insert your login!");
}
else{
List<NameValuePair> urlParameters = new ArrayList<NameValuePair>();
urlParameters.add(new BasicNameValuePair("username", nUsername));
urlParameters.add(new BasicNameValuePair("password", nPassword));
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost(SERVER_PROXY);
post.setHeader("User-Agent", SERVER_USER_AGENT);
try{
post.setEntity(new UrlEncodedFormEntity(urlParameters, "utf-8"));
}
catch(Exception e){
Log.getStackTraceString(e);
}
HttpResponse response = null;
try{
response = client.execute(post);
Log.e("Response Code : ", " = " + response.getStatusLine().getReasonPhrase());
InputStream inputStream = response.getEntity().getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"));
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
inputStream.close();
String responseMessage = sb.toString();
checkLogin(responseMessage);
}
catch(Exception e){
Log.e("HTTP-ERROR", " = " + Log.getStackTraceString(e));
displayToast("Check your Internet connection!");
}
}
}
当我在PHP脚本中回显它时,字符串responseMessage包含我的blob数据。我只需要将这个流放在一个Bitmap或其他东西中,但我不知道如何完成它。
谢谢,如果你知道的话!
答案 0 :(得分:1)
您需要更改客户端和服务器代码。
在PHP脚本的末尾添加此内容
header("Content-Type: image/png");
header("Content-Length: " . mb_strlen($blob_data, '8bit'));
echo $blob_data;
exit;
您的代码尚未准备好读取二进制数据,请查看有关如何执行此操作的其他问题:how to download image from any web page in java
答案 1 :(得分:1)
php代码(服务器)
<?php
header('Content-Type: image/png');
echo($blob_data);
?>
java代码(设备)
URL url = new URL ("http://myserver.com/myPicture.png");
InputStream input = url.openStream();
try {
//The sdcard directory e.g. '/sdcard' can be used directly, or
//more safely abstracted with getExternalStorageDirectory()
File storagePath = Environment.getExternalStorageDirectory();
OutputStream output = new FileOutputStream (new File(storagePath,"myPicture.png"));
try {
byte[] buffer = new byte[aReasonableSize];
int bytesRead = 0;
while ((bytesRead = input.read(buffer, 0, buffer.length)) >= 0) {
output.write(buffer, 0, bytesRead);
}
} finally {
output.close();
}
} finally {
input.close();
}
<强>清单强>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
答案 2 :(得分:1)
不建议将实际文件发送到您的Android应用程序,最好的方法是发送图像的 base 64编码 a link字符串,这将包含所有内容图像的元数据,然后从base46编码的字符串加载图像
$image = base64_decode($file_path);
return response::json(array('image'=>$image);
并像对象一样接收它并使用base64code图像渲染图像,如下所示 我喜欢这样的
var reader = new FileReader();
reader.onload = function(e) {
image_base64 = e.target.result;
preview.html("<img src='" + image_base64 + "'/>");
};
reader.readAsDataURL(file);