私は直接はしませんでしたが、回避しました。私はPHPでbase64文字列に画像を変換しました。アンドロイドでは、POSTユーザーID &にStringRequestを使用してエンコードされた文字列を受信し、デコードされた文字列&をイメージに変換し直しました。
PHPコード: userPhoto.php
<?php
...
$userId = htmlentities($_POST["userId"]);
...
$userImage= $userPhoto["user_photo"];
$returnValue["userIdEncoded"] = base64_encode($userImage);
...
echo json_encode($returnValue);
return;
?>
のAndroidコード: は、データベースからデータを取得するために、新しいJavaファイルを作成:
:画像を保存するMainActivityに次のコード
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.toolbox.StringRequest;
import java.util.HashMap;
import java.util.Map;
public class PhotoRequest extends StringRequest {
private static final String photoRequestUrl = "http://.../php/userPhoto.php";
private Map<String, String> params;
public PhotoRequest (String userId, Response.Listener<String> listener, Response.ErrorListener errorListener){
super(Request.Method.POST, photoRequestUrl, listener, errorListener);
params = new HashMap<>();
params.put("userId", userId);
}
@Override
public Map<String, String> getParams() {
return params;
}
}
を追加しました
Response.Listener<String> photoResponseListener = new Response.Listener<String>() {
@Override
public void onResponse(String imageResponse) {
try {
JSONObject jsonResponse = new JSONObject(imageResponse);
final String userPhotoEncodedString = jsonResponse.getString("userIdEncoded");
final byte[] decodedString = Base64.decode(userPhotoEncodedString, Base64.DEFAULT);
Bitmap decodedImage = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
ContextWrapper cw = new ContextWrapper(getApplicationContext());
File imageDirectory = cw.getDir("imageDir", Context.MODE_PRIVATE);
boolean isDirectoryExists = imageDirectory.exists();
if (!isDirectoryExists) {
isDirectoryExists = imageDirectory.mkdir();
}
System.out.println("isDirectoryExists:" + isDirectoryExists);
File imagePath = new File(imageDirectory, "userPhoto.png");
boolean isFileExists = imagePath.exists();
if(isFileExists){
isFileExists = imagePath.delete();
}
System.out.println(isFileExists);
FileOutputStream fos;
try {
fos = new FileOutputStream(imagePath);
decodedImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e) {
e.printStackTrace();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
PhotoRequest photoRequest = new PhotoRequest(userId, photoResponseListener, null);
RequestQueue queue = Volley.newRequestQueue(LoginPage.this);
queue.add(photoRequest);
次のコードを表示する保存したファイルの画像:
final ImageView ivUserPhoto = (ImageView) findViewById(R.id.ivUserPhoto);
try {
ContextWrapper cw = new ContextWrapper(getApplicationContext());
File imageDirectory = cw.getDir("imageDir", Context.MODE_PRIVATE);
File imagePath = new File(imageDirectory, "userPhoto.png");
Bitmap userImage = BitmapFactory.decodeStream(new FileInputStream(imagePath));
ivUserPhoto.setImageBitmap(userImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
ダウンロードはPOSTメソッドでは行われません。ダウンロード要求のみがサーバーにPOSTEDされます。その後、ファイルはストリームを介して送信され、したがって受信されます。 – greenapps
GETリクエストのPHPコードを表示するだけです。 POSTで動作させるためには、少し変更する必要があります。あなたはGETとPOSTの両方がうまく動作するように非常に簡単にすることもできます。 – greenapps
またはあなたのPHPスクリプトはPOSTで正常に動作し、あなたはAndroidコードを求めているだけですか?不明。 – greenapps