Php Android:如何从远程服务器获取图像

Php Android:如何从远程服务器获取图像,php,android,json,wamp,Php,Android,Json,Wamp,我正在开发一个安卓应用程序,可以从远程服务器上获取图像。我使用WAMP作为服务器,PHP作为编程语言。我知道如何使用JSON获取文本数据 我没有使用blob来存储图像。 图像已存储在服务器上的文件夹中。图像的Url存储在db表中 我尝试了下面的代码片段,我从网络上得到了这个,但它没有给出任何错误,也没有显示图像 HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost("http://10.

我正在开发一个安卓应用程序,可以从远程服务器上获取图像。我使用WAMP作为服务器,PHP作为编程语言。我知道如何使用JSON获取文本数据

我没有使用blob来存储图像。 图像已存储在服务器上的文件夹中。图像的Url存储在db表中

我尝试了下面的代码片段,我从网络上得到了这个,但它没有给出任何错误,也没有显示图像

HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("http://10.0.2.2/sareesProject/returnSareeTypeImageUrls.php");
response = httpClient.execute(httpPost);
entity = response.getEntity();
if(response.getStatusLine().getStatusCode() == 200)
{
    Log.d("Http Response:", response.toString());
    if(entity != null)
    {
        InputStream instream = entity.getContent();
        JSONObject  jsonObj = new JSONObject(convertStreamToString(instream));
        String base64Image = jsonObj.getString("pprs");
        Toast.makeText(getBaseContext(), base64Image, Toast.LENGTH_LONG).show();
        byte[] rawImage = Base64.decode(base64Image, Base64.DEFAULT);
        bmp = BitmapFactory.decodeByteArray(rawImage, 0, rawImage.length);
    }
}
ImageView imageview = (ImageView) findViewById(R.id.flag);
imageview.setImageBitmap(bmp);
下面是我的php代码

 <?php
    error_reporting( E_ALL ^ E_NOTICE ^ E_WARNING);
    $con = mysql_connect("localhost","root","") or die("con error");
    mysql_select_db("sareesdb") or die("db select eror");
    $query = mysql_query("select * from noofpiecesinatype");
    if($row = mysql_fetch_assoc($query))
    {
        $response = $row['imageUrl'];
    }
    $response = base64_encode($response);
    echo '{"pprs":'.json_encode($response).'}';
    mysqli_close($con);
    ?>
<?php
error_reporting( E_ALL ^ E_NOTICE ^ E_WARNING);
$con = mysql_connect("localhost","root","") or die("con error");
mysql_select_db("sareesdb") or die("db select eror");
$query = mysql_query("select * from noofpiecesinatype");
$response = array();
while($row = mysql_fetch_assoc($query))
{
$response[] = $row['imageUrl'];
}
echo json_encode($response);
mysqli_close($con);
?>


我用html检查了我的php代码(没有编码$response值),我在那里得到了图像,但在Android中没有。

我不擅长php,但是如果你通过JSON响应返回文件url,你可以使用以下代码下载文件

int count;
        try {
            URL url = new URL("http://url of your file");
            URLConnection conection = url.openConnection();
            conection.connect();
            // getting file length
            int lenghtOfFile = conection.getContentLength();

            // input stream to read file - with 8k buffer
            InputStream input = new BufferedInputStream(url.openStream(), 8192);

            // Output stream to write file
            OutputStream output = new FileOutputStream("/sdcard/downloadedfile.jpg");

            byte data[] = new byte[1024];

            long total = 0;

            while ((count = input.read(data)) != -1) {
                total += count;
                // publishing the progress....
                // After this onProgressUpdate will be called
                publishProgress(""+(int)((total*100)/lenghtOfFile));

                // writing data to file
                output.write(data, 0, count);
            }

            // flushing output
            output.flush();

            // closing streams
            output.close();
            input.close();

        } catch (Exception e) {
            Log.e("Error: ", e.getMessage());
        }
##编辑## 下载图像后,您可以从图像路径/InputStream创建位图,并将其分配给图像视图,如下所示

 BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);

Bitmap bmp = BitmapFactory.decodeStream(bufferedInputStream);

下面是我的php代码

 <?php
    error_reporting( E_ALL ^ E_NOTICE ^ E_WARNING);
    $con = mysql_connect("localhost","root","") or die("con error");
    mysql_select_db("sareesdb") or die("db select eror");
    $query = mysql_query("select * from noofpiecesinatype");
    if($row = mysql_fetch_assoc($query))
    {
        $response = $row['imageUrl'];
    }
    $response = base64_encode($response);
    echo '{"pprs":'.json_encode($response).'}';
    mysqli_close($con);
    ?>
<?php
error_reporting( E_ALL ^ E_NOTICE ^ E_WARNING);
$con = mysql_connect("localhost","root","") or die("con error");
mysql_select_db("sareesdb") or die("db select eror");
$query = mysql_query("select * from noofpiecesinatype");
$response = array();
while($row = mysql_fetch_assoc($query))
{
$response[] = $row['imageUrl'];
}
echo json_encode($response);
mysqli_close($con);
?>

//-------------------- 最后我拿到了

首先,我的服务器文件返回以下内容 {“pprs”:“upload/22.png”} 从中,我使用JSON提取了upload/22.png 现在bitmapPath包含upload/22.png

非常感谢失眠症患者提供建议


如果有任何一票对我有帮助………

谢谢你的回复。。。。。。我认为这个代码可以用来存储sd卡上的图像。我不想存储它,我需要将图像设置为ImageView列表ImageView=(ImageView)findViewById(R.id.flag);设置图像位图(bmp);然后可以从Inputstream创建位图并将其分配给listViewk。InputStream instream=entity.getContent();在这个阶段,我有InputStream。你能告诉我如何从这个输入流创建位图吗。我还尝试了这个bmp=BitmapFactory.decodeByteArray(rawImage,0,rawImage.length);我尝试使用bmp=BitmapFactory.decodeStream(流内);但是它返回null…………文件正在下载吗?如果是,您是否尝试将其保存到/sdcard?检查此。。这对你会有帮助的……好例子。如果我们想使用上面的示例在列表视图中显示图像,我们可以这样做吗?