I have a Image in my own Database like this
And I use this PHP code to display that Image in Browser, it work well.
<?php
require_once __DIR__ . '/db_config1.php';
$con = mysqli_connect(DB_SERVER, DB_USER, DB_PASSWORD);
if(mysqli_connect_errno()){
echo "connect failed: %s
", 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";
}
?>
Here is Image's url: http://kenkma.freevnn.com/android_connect/getImage1.php?pid=118
My manifest also have this
<uses-permission android:name="android.permission.INTERNET" />
In Android studio I try to use this codes to display that Image url in Image View
//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;
}
}
And I try using universal-image-loader and Picasso librarys like this.
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);
All of theme work well with all Image's url I get on Internet (google Image or Image in some website upload Image) but It not work for my Image's url (Image always return Null and do not dipslay any thing in Image View).