无法将图片从网址加载到Android图片视图

时间:2015-11-25 11:47:42

标签: php android database loadimage

我在自己的数据库中有一个像这样的

Image Database

我使用这个PHP代码在浏览器中显示该图像,它运行良好。

<?php
    require_once __DIR__ . '/db_config1.php';
    $con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD);
    if(mysqli_connect_errno()){
        echo "connect failed: %s\n", mysqli_connect_error;
    }
    $db = mysqli_select_db($con, DB_DATABASE) or die(mysqli_error($con));
    if (isset($_GET['pid'])) {
        $pid = $_GET['pid'];

        $result = mysqli_query($con, "SELECT * FROM products WHERE pid = $pid");
        if (!empty($result)) {
            $result = mysqli_fetch_array($result);
        }
        header('content-type: image/png');
        echo $result['image'];
        mysqli_close($con);
    }else{
        echo "Error";
    }   
?>

以下是图片网址:http://kenkma.freevnn.com/android_connect/getImage1.php?pid=118

我的清单也有这个

<uses-permission android:name="android.permission.INTERNET" />

在Android工作室中,我尝试使用此代码在图像视图中显示图像网址

//Using in AsynTask
class DownloadImageTask extends AsyncTask<String, Void, Bitmap> {
    private Bitmap image;
    private CircleImageView civ;

    public DownloadImageTask(CircleImageView civ) {
        this.civ = civ;
    }

    protected Bitmap doInBackground(String... urls) {
        String urldisplay = urls[0];
        try {
            InputStream in = new URL(urldisplay).openStream();
            image = BitmapFactory.decodeStream(in);
        } catch (Exception e) {
            image = null;
        }
        return image;
    }
    protected void onPostExecute(Bitmap result) {
        if (result != null) {
            civ.setImageBitmap(result);
        }
    }
}

//or try use this
public Bitmap getBitmapFromURL(String src) {
    try {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = new BufferedInputStream(connection.getInputStream());
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;

    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
        return null;
    }
}

我尝试使用像这样的通用图像加载器和毕加索图书馆。

String url = "http://kenkma.freevnn.com/android_connect/getImage1.php?pid=118"
civ = (CircleImageView) findViewById(R.id.profile_image);

//With Image Loader lib
ImageLoader imageLoader = ImageLoader.getInstance();  
imageLoader.init(ImageLoaderConfiguration.createDefault(MainActivity.this));
imageLoader.displayImage(url, civ);

//Or with Picasso lib
Picasso.with(MainActivity.this)
            .load(url) 
            .placeholder(R.drawable.logo)
            .error(R.mipmap.ic_launcher)
            .into(civ);

所有主题都适用于我上网的所有图片网址(谷歌图片或图片在某些网站上传图片),但它不适用于我的图片网址(图片总是返回Null和do在图像视图中没有任何东西。

0 个答案:

没有答案