2016-07-17 6 views
0

POSTメソッドを使用してデータベースからアプリケーションにユーザーイメージをダウンロードする必要があります。ここで私はuserIdをPOSTメソッドでPHPに渡し、レスポンスとしてイメージを取得する必要があります。この画像を保存してImageViewに表示します。私はすでにiOSでこれをやっていましたが、Androidの場合は助けが必要です。POSTメソッドを使用してデータベースから画像をダウンロードする方法

私は多くのGoogle検索を行っていますが、GETメソッドまたは直接URLメソッドを使用して画像をダウンロードするチュートリアルは見つかりましたが、POSTメソッドはありません。 POSTメソッドを使ってサンプルコードを手伝ってください。 私は既にiosのために使用しているPHPファイルは以下です。

<?php 

require("userdao.php"); 
... 
$userId = htmlentities($_POST["userId"]); 

$returnValue = array(); 
$dao = new userdao($dbhost, $dbuser, $dbpass, $dbname); 
$dao->openConnection(); 
$userPhoto = $dao->getUserPhoto($userId); 
$returnValue = $userPhoto["user_photo"]; 
$dao->closeConnection(); 

echo ($returnValue); 
return; 
?> 

編集:ここではいくつかの追加情報があり、 画像は、BLOBを使用してデータベースに格納されています。上記のphpはユーザID入力を必要とします&は、テーブルから対応するイメージを取得し、それからちょうどそれをエコーし​​ます。ここにjson_encodeはありません。私は文字列リクエストをしているこのアプリケーションのための他のPHPを持っています&は、すべての罰金を&ボレーを使用して返信を取得しています。しかし、この特定の1つ私は応答として画像が必要です、私は画像メソッドまたはBitmap Requestのコードを見つけることができません。&イメージレスポンスを取得する1つのパラメータを送ることができます。 このphpはios & androidに共通しています。私はこの作業をするためにアンドロイド用のコードが必要です。 さらに多くの検索の後、私はこの質問を投稿しました。どんな助けもありがとうございます。

+0

ダウンロードはPOSTメソッドでは行われません。ダウンロード要求のみがサーバーにPOSTEDされます。その後、ファイルはストリームを介して送信され、したがって受信されます。 – greenapps

+0

GETリクエストのPHPコードを表示するだけです。 POSTで動作させるためには、少し変更する必要があります。あなたはGETとPOSTの両方がうまく動作するように非常に簡単にすることもできます。 – greenapps

+0

またはあなたのPHPスクリプトはPOSTで正常に動作し、あなたはAndroidコードを求めているだけですか?不明。 – greenapps

答えて

0

私は直接はしませんでしたが、回避しました。私は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(); 
} 
+0

上記のコードを追加しました。 – Karthik

関連する問題