doulianglou0898 2014-01-18 19:48
浏览 137
已采纳

如何从HttpClient中检索照片?

I am using HttpClient to make a request from my Android device to a simple HTTP server, which contains a PHP script to retrieve a picture. The picture is available inside a blob data. It means if I do this little PHP code in the server side:

 file_put_contents("myPicture.png", $blob_data);

I get the picture saved in the server with a file named myPicture.png. Now I want to get this $blob_data (my picture) to save it inside my Android device, not the HTTP server. Someone can give me a hint to return the blob data from the PHP script and get the picture inside the Android device to store it locally?

Here is my 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 + "
");
            }
            inputStream.close();
            String responseMessage = sb.toString();
            checkLogin(responseMessage);
        }
        catch(Exception e){
            Log.e("HTTP-ERROR", " = " + Log.getStackTraceString(e));
            displayToast("Check your Internet connection!");
        }
    }
}

String responseMessage contains my blob data when I echo it in my PHP script. I just need to put this stream inside a Bitmap or something, but I've no clue about how to get it done.

Thanks if you know it!

  • 写回答

3条回答 默认 最新

  • drzrzzkh462254 2014-01-26 19:57
    关注

    php code (server)

    <?php
    header('Content-Type: image/png');
    echo($blob_data);
    ?>
    

    java code (device)

    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();
    }
    

    Manifest

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么